Same function over multiple data frames in R

前端 未结 4 1463
南笙
南笙 2020-11-29 03:08

I am new to R, and this is a very simple question. I\'ve found a lot of similar things to what I want but not exactly it. Basically I have multiple data frames and I simply

4条回答
  •  醉酒成梦
    2020-11-29 03:54

    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.

提交回复
热议问题