R 3.0.3 rbind multiple csv files

后端 未结 2 1892
失恋的感觉
失恋的感觉 2020-12-19 08:19

R 3.0.3: I have 40 csv files all structured the same that I want to rbind into one file so I can calculate the mean of one column.

I searched:

相关标签:
2条回答
  • 2020-12-19 09:05

    Using the answer from here [Importing several files and indexing them ]

    list files with .csv extension - this assumes that the only .csv files in your working directory are the ones you want to read

    files  <- list.files(pattern = '\\.csv')
    

    read files into a list - are there headers?

    tables <- lapply(files, read.csv, header = TRUE)
    

    rbind files

    combined.df <- do.call(rbind , tables)
    

    You can then find the mean - find which columns are numeric

    s <- sapply(combined.df, is.numeric)
    

    find the mean of numeric variables

    colMeans(combined.df[s])
    
    0 讨论(0)
  • 2020-12-19 09:06

    In more contemporary plyr approach:

    files <- list.files(...)
    data <- adply(files, 1, read.table)
    

    (it's saturday afternoon: untested code, but the approach is fine)

    0 讨论(0)
提交回复
热议问题