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

那年仲夏 提交于 2019-12-01 01:59:11

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))

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.

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