apply a function over groups of columns

后端 未结 6 846
梦谈多话
梦谈多话 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:49

    mean for rows from vectors a,b,c

     rowMeans(dat[1:3])
    

    means for rows from vectors d,e,f

     rowMeans(dat[4:6])
    

    all in one call you get

    results<-cbind(rowMeans(dat[1:3]),rowMeans(dat[4:6]))
    

    if you only know the names of the columns and not the order then you can use:

    rowMeans(cbind(dat["a"],dat["b"],dat["c"]))
    rowMeans(cbind(dat["d"],dat["e"],dat["f"]))
    
    #I dont know how much damage this does to speed but should still be quick
    

提交回复
热议问题