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”的新变量中创建基于它们所在的“Batch”的“文件”组来遍历整个数据集(有十几个“Batch”类别)

IE

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

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

如何为完整数据集中的所有批次组执行此操作?

4

1 回答 1

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
于 2012-05-28T14:55:04.570 回答