Same function over multiple data frames in R

I am new to R and this is a very simple question. I found a lot of similar things that I want, but not quite like that. Basically, I have several frames of data, and I just want to run the same function for all of them. It may work for a loop, but I'm not sure how to properly configure it to call data frames. It also seems that most prefer a lappish approach with R. I played with the get function, but to no avail. I apologize if this is a duplicate question. Any help would be greatly appreciated!

Here is my simplified simplified example: 2 data frames: df1, df2

df1 start stop ID 0 10 x 10 20 y 20 30 z df2 start stop ID 0 10 a 10 20 b 20 30 c 

what i want is 4th column with average start and stop value for both dfs

 df1 start stop ID Avg 0 10 x 5 10 20 y 15 20 30 z 25 

I can make this one data frame at a time:

 df1$Avg <- rowMeans(subset(df1, select = c(start, stop)), na.rm = TRUE) 

but I want to run it on all data frames.

+7
for-loop r dataframe lapply
source share
3 answers

Make a list of data frames, then use lapply to apply the function to them.

 df.list <- list(df1,df2,...) res <- lapply(df.list, function(x) rowMeans(subset(x, select = c(start, stop)), na.rm = TRUE)) # to keep the original data.frame also res <- lapply(df.list, function(x) cbind(x,"rowmean"=rowMeans(subset(x, select = c(start, stop)), na.rm = TRUE))) 

Then the tape will be loaded into each data frame as x sequentially.

+7
source share

Put them in a list, and then run rowMeans through the list.

 df1 <- data.frame(x = rep(3, 5), y = seq(1, 5, 1), ID = letters[1:5]) df2 <- data.frame(x = rep(5, 5), y = seq(2, 6, 1), ID = letters[6:10]) lapply(list(df1, df2), function(w) { w$Avg <- rowMeans(w[1:2]); w }) [[1]] xy ID Avg 1 3 1 a 2.0 2 3 2 b 2.5 3 3 3 c 3.0 4 3 4 d 3.5 5 3 5 e 4.0 [[2]] xy ID Avg 1 5 2 f 3.5 2 5 3 g 4.0 3 5 4 h 4.5 4 5 5 i 5.0 5 5 6 j 5.5 
+5
source share

If you need all the outputs in a single file, this can help.

  df1 <- data.frame(x = rep(3, 5), y = seq(1, 5, 1), ID = letters[1:5]) df2 <- data.frame(x = rep(5, 5), y = seq(2, 6, 1), ID = letters[6:10]) z=list(df1,df2) df=NULL for (i in z) { i$Avg=(i$x+i$y)/2 df<-rbind(df,i) print (df) } > df xy ID Avg 1 3 1 a 2.0 2 3 2 b 2.5 3 3 3 c 3.0 4 3 4 d 3.5 5 3 5 e 4.0 6 5 2 f 3.5 7 5 3 g 4.0 8 5 4 h 4.5 9 5 5 i 5.0 10 5 6 j 5.5 
+1
source share

All Articles