R: combine summary of multiple csv files into one data frame

让人想犯罪 __ 提交于 2019-12-11 03:45:14

问题


I have a directory with many csv files. I want to read them into then create a summary of a column into one data frame.

getSummary<-function(df){
   summary(df$Names)
}

filenames<-list.files(path, pattern="*.csv", full.names=T)
ldf<-lapply(filenames, read.csv, header=T)
results<-lapply(ldf, getSummary)

It is returning results as follows:

print(results)
[[1]]  a   b   c  d
      100  2   3  4
[[2]]  a   b   c  d
      97   4   2  6

What I want is something like this

filename  a   b   c   d
1.csv    100  2   3   4
2.csv     97  4   2   6

Can someone help??

Thanks.


回答1:


I think you probably want do.call(rbind,results). But that will only work if each component has at least one of each letter (i.e. all the columns must be present in each). If not you could use rbind.fill from plyr instead.

I'd also suggest that table is a safer function to be using for this purpose than summary.



来源:https://stackoverflow.com/questions/16984529/r-combine-summary-of-multiple-csv-files-into-one-data-frame

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