简体   繁体   中英

Apply series of changes to multiple similar datasets in R

I have 20 csv files of data that are formatted exactly the same, about 40 columns of different numbers, but with different values in each column. I want to apply a series of changes to each data frame in order to extract specific information from every one of them.

Specifically I want to extract four columns from each data frame, find the maximum value of each column in each data frame and then add all of these maximum values together, so I get one final number for each data frame. Something like this:

str(data)    
Extract<-data[c(1,2,3,4)]
Max<-apply(Extract,2,max)
Add<-Max[1] + Max[2] + Max[3] + Max[4]

I have the code written above to do all these steps for every data frame individually, but is it possible to apply this code to all of them at once?

If you put all 20 filenames into a vector called files

Maxes <- numeric(length(files))
i <- 1

for (file in files) {

     data <- read.csv(file)
     str(data)    
     Extract<-data[c(1,2,3,4)]
     Max<-apply(Extract,2,max)
     Add<-Max[1] + Max[2] + Max[3] + Max[4]
     Maxes[i] <- Add
     i <- i+1

}

Though that str(data) will just cause a lot of stuff to print to the terminal 20 times. I'm not sure the value of that, but it was in your question so I included.

Put all your files into a common folder such as /path/temp/

csvs <- list.files("/path/temp")   # vector of csv

Use custom function for colMax

colMax <- function(data) sapply(data, max, na.rm = TRUE)

Using foreach , dplyr , and readr

library(foreach)
library(dplyr)

foreach(i=1:length(csvs), .combine="c") %do% { read_csv(csvs[i]) %>%
                                                select(1:4) %>% 
                                                colMax(.) %>% 
                                                sum(.)
                                             }  # returns a vector

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM