apply a function over groups of columns

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

    A similar question was asked here by @david: averaging every 16 columns in r (now closed), which I answered by adapting @TylerRinker's answer above, following a suggestion by @joran and @Ben. Because the resulting function might be of help to OP or future readers, I am copying that function here, along with an example for OP's data.

    # Function to apply 'fun' to object 'x' over every 'by' columns
    # Alternatively, 'by' may be a vector of groups
    byapply <- function(x, by, fun, ...)
    {
        # Create index list
        if (length(by) == 1)
        {
            nc <- ncol(x)
            split.index <- rep(1:ceiling(nc / by), each = by, length.out = nc)
        } else # 'by' is a vector of groups
        {
            nc <- length(by)
            split.index <- by
        }
        index.list <- split(seq(from = 1, to = nc), split.index)
    
        # Pass index list to fun using sapply() and return object
        sapply(index.list, function(i)
                {
                    do.call(fun, list(x[, i], ...))
                })
    }
    

    Then, to find the mean of the replicates:

    byapply(dat, 3, rowMeans)
    

    Or, perhaps the standard deviation of the replicates:

    byapply(dat, 3, apply, 1, sd)
    

    Update

    by can also be specified as a vector of groups:

    byapply(dat, c(1,1,1,2,2,2), rowMeans)
    

提交回复
热议问题