在for循环中对表进行子集化并将它们保存为csv

时间:2016-05-24 05:38:21

标签: r

我是R的新手并且正在尝试使用它来生成大量的子集(> 4000),这是我在JSL中的当前脚本无法处理的。我非常感谢如果有人可以给我指点如下所示的表格,并使用列名和行信息将它们保存为Navigation Item

.csv

我希望通过set.seed(24) DT = data.frame(Group=c("A","A","A","A","B","B","B","B","B"),Output1 = rnorm(9),Output2=rnorm(9),Output3=rnorm(9),Step=c("0","0","1","1","1","1","2","2","2")) DT 对其进行子集化,将步骤保存为具有特定命名约定的Group,Output[i]文件" .csv"。示例如下所示

column_Step_Group.csv

2 个答案:

答案 0 :(得分:1)

或循环

DT = data.frame(Group=c("A","A","A","A","B","B","B","B","B"),Output1 = rnorm(9),Output2=rnorm(9),Output3=rnorm(9),Step=c("0","0","1","1","1","1","2","2","2"))

for(i in levels(DT$Group)) {
  da <- subset(DT, Group == i)
  write.csv(da, paste("Group_", i, ".csv", sep = ""),  row.names = FALSE, quote = FALSE)
}

答案 1 :(得分:0)

我们可以使用split创建listdata.frame。循环遍历'lst'(lapply(names(lst),...)的名称并使用fwrite中的data.table(以便更快地写入)。

lst <- split(DT[-c(1,5)], list(DT$Group, DT$Step), drop=TRUE)
library(data.table)
invisible(lapply(names(lst), function(x) {
      l1 <- split.default(lst[[x]], names(lst[[x]]))
      lapply(names(l1), function(y)  fwrite(l1[[y]],
             paste0(y, "_", x, ".csv")))}))

“Output1_A.0.csv”的输出

 Output1
-0.545880758
0.536585304