consolidating data frames in R

后端 未结 2 850
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 08:23

Hi I have a lot of CSV files to process. Each file is generated by a run of an algorithm. My data always has one key and a value like this:

csv1:

            


        
2条回答
  •  一生所求
    2020-12-03 09:15

    Here is a solution. I am following all the excellent comments so far, and hopefully adding value by showing you how to handle any number of files. I am assuming you have all your csv files in the same directory (my.csv.dir below).

    # locate the files
    files <- list.files(my.csv.dir)
    
    # read the files into a list of data.frames
    data.list <- lapply(files, read.csv)
    
    # concatenate into one big data.frame
    data.cat <- do.call(rbind, data.list)
    
    # aggregate
    data.agg <- aggregate(value ~ index, data.cat, mean)
    

    Edit: to handle your updated question in your comment below:

    files     <- list.files(my.csv.dir)
    algo.name <- sub("-.*", "", files)
    data.list <- lapply(files, read.csv)
    data.list <- Map(transform, data.list, algorithm = algo.name)
    data.cat  <- do.call(rbind, data.list)
    data.agg  <- aggregate(value ~ algorithm + index, data.cat, mean)
    

提交回复
热议问题