如何将向量分组到向量列表中? [英] How to group a vector into a list of vectors?
问题描述
我有一些看起来像这样的数据(例如,假数据):
I have some data which looks like this (fake data for example's sake):
dressId color
6 yellow
9 red
10 green
10 purple
10 yellow
12 purple
12 red
其中颜色是因子向量.不能保证因子的所有可能水平实际上都出现在数据中(例如,蓝色"颜色也可以是水平之一).
where color is a factor vector. It is not guaranteed that all possible levels of the factor actually appear in the data (e.g. the color "blue" could also be one of the levels).
我需要一个向量列表,用于将每件衣服的可用颜色分组:
I need a list of vectors which groups the available colors of each dress:
[[1]]
yellow
[[2]]
red
[[3]]
green purple yellow
[[4]]
purple red
保留礼服的ID会很好(例如,一个数据框,其中此列表为第二列,ID为第一列),但这不是必需的.
Preserving the IDs of the dresses would be nice (e.g. a dataframe where this list is the second column and the IDs are the first), but not necessary.
我写了一个循环,遍历数据帧的每一行,当下一个ID相同时,它将颜色添加到向量中. (我确定数据按ID排序).当第一列中的ID更改时,它会将矢量添加到列表中:
I wrote a loop which goes through the dataframe row for row, and while the next ID is the same, it adds the color to a vector. (I am sure that the data is sorted by ID). When the ID in the first column changes, it adds the vector to a list:
result <- NULL
while(blah blah)
{
some code which creates the vector called "colors"
result[[dressCounter]] <- colors
dressCounter <- dressCounter + 1
}
在努力使所有必要的计数变量正确无误后,我感到沮丧的是,它不起作用.第一次colors
是
After wrestling with getting all the necessary counting variables correct, I found out to my dismay that it doesn't work. The first time, colors
is
[1] yellow
Levels: green yellow purple red blue
并将其强制为整数,因此result
变为2
.
and it gets coerced into an integer, so result
becomes 2
.
在第二个循环重复中,colors
仅包含红色,并且result
成为简单的整数矢量[1] 2 4
.
In the second loop repetition, colors
only contains red, and result
becomes a simple integer vector, [1] 2 4
.
在第三次重复中,colors
现在是向量
In the third repetition, colors
is a vector now,
[1] green purple yellow
Levels: green yellow purple red blue
我得到
result[[3]] <- colors
结果错误[[3]]<-颜色:
提供的元素多于要替换的元素
Error in result[[3]] <- colors :
more elements supplied than there are to replace
我做错了什么?有没有一种初始化result
的方法,这样它就不会转换成数值向量,而是变成向量列表?
What am I doing wrong? Is there a way to initialize result
so it doesn't get converted into a numeric vector, but becomes a list of vectors?
此外,除了自己动手"之外,还有另一种方法来完成整个工作吗?
Also, is there another way to do the whole thing than "roll my own"?
推荐答案
split.data.frame
是组织此问题的好方法;然后提取颜色分量.
split.data.frame
is a good way to organize this; then extract the color component.
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")))
我认为您想要的版本实际上是这样的:
I think the version you want is actually this:
ss <- split.data.frame(d,d$dressId)
通过提取颜色分量,您可以得到更像您要求的列表的东西:
You can get something more like the list you requested by extracting the color component:
lapply(ss,"[[","color")
这篇关于如何将向量分组到向量列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!