Same function over multiple data frames in R

前提是你 提交于 2019-11-28 06:35:42

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

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)))

The lapply will then feed in each data frame as x sequentially.

Put them into a list and then run rowMeans over 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]]
   x y 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]]
   x y 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

In case you want all the outputs in the same file this may 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
   x y 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!