Tuesday, September 22, 2015

Read Multiple CSV files using lapply, rbind & reduce functions - R programming

let's do a quick demo on how lapply works in R programming. Suppose you have a number of csv files and each csv file have same structure.
Here is sample data in 1 csv .

"Date","sulfate","nitrate","ID"
"2009-01-01",NA,NA,69
"2009-01-02",NA,NA,69
"2009-01-03",NA,NA,69
"2009-01-04",NA,NA,69
"2009-01-05",NA,NA,69
"2009-01-06",NA,NA,69


Now you want to read all csv files and calculate mean for 1 column. In this case , let's calculate mean  of "sulphate" column across number of different files.

Step 1: read multiple files and create dataframes.
files<- list.file()



 // here files is collection of all files

//  read.csv is the function that you want to apply on all files

dataFrames <- lapply(files, read.csv)

Step 2: combine all dataframes into 1 single dataframe

dataFrames <- Reduce(function(x, y) rbind(x, y), dataFrames)

Step 3: Calculate mean on dataframe column

mean(dataFrame[,sulphate], na.rm = TRUE)

 

No comments:

Post a Comment