如何将矢量分组到矢量列表?

rum*_*cho 7 grouping r list vector

我有一些看起来像这样的数据(例如假数据):

dressId        color 
6              yellow 
9              red
10             green 
10             purple 
10             yellow 
12             purple 
12             red 
Run Code Online (Sandbox Code Playgroud)

其中颜色是因子向量.不能保证该因子的所有可能水平实际上都出现在数据中(例如,颜色"蓝色"也可以是其中一个水平).

我需要一个矢量列表,将每件衣服的可用颜色分组:

[[1]]
yellow  

[[2]] 
red    

[[3]] 
green purple yellow 

[[4]] 
purple red 
Run Code Online (Sandbox Code Playgroud)

保留连衣裙的ID会很好(例如,这个列表是第二列的数据帧,ID是第一列),但不是必需的.

我写了一个循环,它遍历行的数据帧行,而下一个ID是相同的,它将颜色添加到矢量.(我确信数据按ID排序).当第一列中的ID更改时,它会将向量添加到列表中:

result <- NULL 
while(blah blah) 
{
    some code which creates the vector called "colors" 
    result[[dressCounter]] <- colors 
    dressCounter <- dressCounter + 1
}
Run Code Online (Sandbox Code Playgroud)

在努力获得所有必要的计数变量之后,我发现我不高兴它不起作用.第一次colors

[1] yellow
Levels: green yellow purple red blue
Run Code Online (Sandbox Code Playgroud)

并且它被强制转换成整数,所以result变成了2.

在第二个循环重复中,colors只包含红色,并result成为一个简单的整数向量[1] 2 4.

在第三次重复中,colors现在是一个向量,

[1] green  purple yellow
Levels: green yellow purple red blue 
Run Code Online (Sandbox Code Playgroud)

我明白了

result[[3]] <- colors
Run Code Online (Sandbox Code Playgroud)

结果错误[[3]] < - 颜色:
提供的元素多于要替换的元素

我究竟做错了什么?有没有办法初始化result所以它不会被转换为数字向量,但成为向量列表?

还有,还有另一种方法来完成整个事情而不是"滚动我自己的"吗?

Ben*_*ker 9

split.data.frame是组织这个的好方法; 然后提取颜色成分.

d <- data.frame(dressId=c(6,9,10,10,10,12,12),
               color=factor(c("yellow","red","green",
                              "purple","yellow",
                              "purple","red"),
                 levels=c("red","orange","yellow",
                          "green","blue","purple")))
Run Code Online (Sandbox Code Playgroud)

我认为你想要的版本实际上是这样的:

ss <- split.data.frame(d,d$dressId)
Run Code Online (Sandbox Code Playgroud)

您可以通过提取颜色组件来获得更像您请求的列表:

lapply(ss,"[[","color")
Run Code Online (Sandbox Code Playgroud)

  • +1如果它只是他们想要的列表(从描述中不清楚)或许最好直接使用`split`并跳过`lapply`步骤. (2认同)

A5C*_*2T1 6

除此之外split,你应该考虑aggregate.使用cI作为聚合函数来获取list列:

out <- aggregate(color ~ dressId, mydf, c)
out
#   dressId                 color
# 1       6                yellow
# 2       9                   red
# 3      10 green, purple, yellow
# 4      12           purple, red
str(out)
# 'data.frame': 4 obs. of  2 variables:
#  $ dressId: int  6 9 10 12
#  $ color  :List of 4
#   ..$ 0: chr "yellow"
#   ..$ 1: chr "red"
#   ..$ 2: chr  "green" "purple" "yellow"
#   ..$ 3: chr  "purple" "red"
out$color
# $`0`
# [1] "yellow"
# 
# $`1`
# [1] "red"
# 
# $`2`
# [1] "green"  "purple" "yellow"
# 
# $`3`
# [1] "purple" "red" 
Run Code Online (Sandbox Code Playgroud)

注意:即使"颜色"变量是afactor,这也适用,就像Ben的样本数据一样(我在上面发布答案时错过了这一点),但你需要I用作聚合函数而不是c:

out <- aggregate(color ~ dressId, d, I)
str(out)
# 'data.frame': 4 obs. of  2 variables:
#  $ dressId: num  6 9 10 12
#  $ color  :List of 4
#   ..$ 0: Factor w/ 6 levels "red","orange",..: 3
#   ..$ 1: Factor w/ 6 levels "red","orange",..: 1
#   ..$ 2: Factor w/ 6 levels "red","orange",..: 4 6 3
#   ..$ 3: Factor w/ 6 levels "red","orange",..: 6 1
out$color
# $`0`
# [1] yellow
# Levels: red orange yellow green blue purple
# 
# $`1`
# [1] red
# Levels: red orange yellow green blue purple
# 
# $`2`
# [1] green  purple yellow
# Levels: red orange yellow green blue purple
# 
# $`3`
# [1] purple red   
# Levels: red orange yellow green blue purple
Run Code Online (Sandbox Code Playgroud)

然而,奇怪的是,默认显示显示整数值:

out
#   dressId   color
# 1       6       3
# 2       9       1
# 3      10 4, 6, 3
# 4      12    6, 1
Run Code Online (Sandbox Code Playgroud)

  • 如何获取字符串而不是整数值? (2认同)

spr*_*9er 5

假设您的数据框保存在名为 的变量中df,那么您可以简单地使用group_by这样summarize的包list函数dplyr

library('dplyr')

df %>%
  group_by(dressId) %>%
  summarize(colors = list(color))
Run Code Online (Sandbox Code Playgroud)

应用于您的示例:

df <- tribble(
  ~dressId, ~color,
         6, 'yellow',
         9, 'red',
        10, 'green',
        10, 'purple',
        10, 'yellow',
        12, 'purple',
        12, 'red'
)

df %>%
  group_by(dressId) %>%
  summarize(colors = list(color))

# dressId                colors
#       6                yellow
#       9                   red
#      10 green, purple, yellow
#      12           purple, red
Run Code Online (Sandbox Code Playgroud)