apply a function over groups of columns

后端 未结 6 843
梦谈多话
梦谈多话 2020-11-28 12:18

How can I use apply or a related function to create a new data frame that contains the results of the row averages of each pair of columns in a very large data

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 12:39

    The rowMeans solution will be faster, but for completeness here's how you might do this with apply:

    t(apply(dat,1,function(x){ c(mean(x[1:3]),mean(x[4:6])) }))
    

提交回复
热议问题