How can I write out multiple files with different filenames in R

三世轮回 提交于 2019-12-19 04:46:15

问题


I have one BIG file (>10000 lines of data) and I want write out a separate file by ID. I have 50 unique ID names and I want a separate text file for each one. Here's what Ive got so far, and I keep getting errors. My ID is actually character string which I would prefer if I can name each file after that character string it would be best.

for (i in 1:car$ID) {
    a <- data.frame(car[,i])
    carib <- car1[,(c("x","y","time","sd"))]
    myfile <- gsub("( )", "", paste("C:/bridge", carib, "_", i, ".txt"))
    write.table(a, file=myfile,
                sep="", row.names=F, col.names=T quote=FALSE, append=FALSE) 
}

回答1:


One approach would be to use the plyr package and the d_ply() function. d_ply() expects a data.frame as an input. You also provide a column(s) that you want to slice and dice that data.frame by to operate on independently of one another. In this case, you have the column ID. This specific function does not return an object, and is thus useful for plotting, or making charter iteratively, etc. Here's a small working example:

library(plyr)

dat <- data.frame(ID = rep(letters[1:3],2) , x = rnorm(6), y = rnorm(6))

d_ply(dat, "ID", function(x)
     write.table(x, file = paste(x$ID[1], "txt", sep = "."), sep = "\t", row.names = FALSE))

Will generate three tab separates files with the ID column as the name of the files (a.txt, b.txt, c.txt).

EDIT - to address follow up question

You could always subset the columns you want before passing it into d_ply(). Alternatively, you can use/abuse the [ operator and select the columns you want within the call itself:

dat <- data.frame(ID = rep(letters[1:3],2) , x = rnorm(6), y = rnorm(6)
  , foo = rnorm(6))

d_ply(dat, "ID", function(x)
     write.table(x[, c("x", "foo")], file = paste(x$ID[1], "txt", sep = ".")
     , sep = "\t", row.names = FALSE))



回答2:


For the data frame called mtcars separated by mtcars$cyl:

lapply(split(mtcars, mtcars$cyl), 
   function(x)write.table(x, file = paste(x$cyl[1], ".txt", sep = "")))

This produces "4.txt", "6.txt", "8.txt" with the corresponding data. This should be faster than looping/subsetting since the subsetting (splitting) is vectorized.



来源:https://stackoverflow.com/questions/5683390/how-can-i-write-out-multiple-files-with-different-filenames-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!