Merging files (and file names) in R

假装没事ソ 提交于 2019-12-04 20:19:49

You can just make a wrapper around the read.table() function that adds in your filename variable. Something like this should work:

read.data <- function(file){
  dat <- read.table(file,header=F,sep=",")
  dat$fname <- file
  return(dat)
}

Once there you just need to apply that function across your data files. Since you didn't post any example data I'm not sure what it actually looks like, but for now I'll assume it's clean as can be and that rbind() is sufficient to join them together, in which case this example should illustrate that function in action:

> data(iris)
> write.csv(iris,file="iris1.csv",row.names=F)
> write.csv(iris,file="iris2.csv",row.names=F)
> dataset <- do.call(rbind, lapply(list.files(pattern="csv$"),read.data))
> head(dataset)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species     fname
1          5.1         3.5          1.4         0.2  setosa iris1.csv
2          4.9         3.0          1.4         0.2  setosa iris1.csv
3          4.7         3.2          1.3         0.2  setosa iris1.csv
4          4.6         3.1          1.5         0.2  setosa iris1.csv
5          5.0         3.6          1.4         0.2  setosa iris1.csv
6          5.4         3.9          1.7         0.4  setosa iris1.csv
> table(dataset$fname)

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