2012-05-28 203 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