用变量分割data.frame [英] Splitting a data.frame by a variable
问题描述
我将来自多个主题的数据存储在一个CSV文件中。导入CSV文件后,我想将每个参与者的数据分割成自己的data.frame。
I have data from several subjects stored in a single CSV file. After importing the CSV file, I would like to split the data from each participant off into its own data.frame.
更确切地说,我想以示例数据为例下面,并创建三个新的data.frames;
More literally, I would like to take the example data below, and create three new data.frames; one for each of the 'subject_initials' values.
我该怎么做?到目前为止,我已经研究了使用 plyr
包和 split()
的选项,但尚未找到解。我知道我可能会遗漏一些明显的东西。
How do I do this? I've thus far looked into options using the plyr
package and split()
, but haven't yet found a solution. I know I'm probably missing something obvious.
推荐答案
split
似乎
如果从以下数据框开始:
If you start with the following data frame :
df <- data.frame(ids=c(1,1,2,2,3),x=1:5,y=letters[1:5])
然后您可以执行以下操作:
Then you can do :
split(df, df$ids)
您将获得数据帧列表:
R> split(df, df$ids)
$`1`
ids x y
1 1 1 a
2 1 2 b
$`2`
ids x y
3 2 3 c
4 2 4 d
$`3`
ids x y
5 3 5 e
这篇关于用变量分割data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!