2012-05-28 205 views
1

我有一个数据集,看起来像一个子列的列表:创建基于另一列

Files   Batch 
filepath1.txt One 
filepath2.txt One 
filepath3.txt One 
filepath4.txt One 
filepath5.txt two 
filepath6.txt two 
filepath7.txt two 
filepath8.txt two 

我想循环在整个数据集(也有一打“批量”类别)的在一个新的变量创建“文件”是基于什么“批量”他们在,一组名为“批量”

batch[1] 
filepath1.txt 
filepath2.txt 
filepath3.txt 
filepath4.txt 

batch[2] 
filepath5.txt 
filepath6.txt 
filepath7.txt 
filepath8.txt 

如何做到这一点对我的所有批次组完整的数据集?

+2

'batch < - with(mydata,split(Files,Batch))'? –

回答

2

split函数似乎是你在找的东西。

> dat <- data.frame(File = paste0("file", 1:10, ".txt"), Batch = rep(c("one", "two"), each = 5)) 
> dat 
     File Batch 
1 file1.txt one 
2 file2.txt one 
3 file3.txt one 
4 file4.txt one 
5 file5.txt one 
6 file6.txt two 
7 file7.txt two 
8 file8.txt two 
9 file9.txt two 
10 file10.txt two 
> split(dat, dat$Batch) 
$one 
     File Batch 
1 file1.txt one 
2 file2.txt one 
3 file3.txt one 
4 file4.txt one 
5 file5.txt one 

$two 
     File Batch 
6 file6.txt two 
7 file7.txt two 
8 file8.txt two 
9 file9.txt two 
10 file10.txt two 
+0

啊,当然。我以前没有用过这样的分割。谢谢! – cianius

+1

如果你只想在你的例子中输出像你这样的输出,那么Ben的评论会让你得到你想要的。但分裂基本上是你想要用于这种情况。 – Dason