问题
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