【问题标题】:R: How to find the mean across categories of variablesR:如何找到变量类别的平均值
【发布时间】:2018-07-24 09:07:22
【问题描述】:

我有一个包含大约 50 个数值变量的数据框。我想创建一个新列,其中包含属于同一类别的一定数量的这些变量的平均值。例如,我可能想创建一个名为df$mean_weight 的新变量,其中包含受访者df$weight1df$weight2df$weight3 行的平均值。高度变量等也是如此。

这是我目前所拥有的:

find_mean = function(...) {
  input_list = list(...)
  output_list = sapply(input_list,mean, na.rm=TRUE)
  return(output_list)
}

df$mean_weight = find_mean(df$weight1, df$weight2, df$weight3)

问题是这给了我一个错误,说替换的行数少于我的原始数据。但是,由于某种原因,当我尝试使用相同代码的高度变量时,不会出现此错误。

【问题讨论】:

    标签: r user-defined-functions mean sapply


    【解决方案1】:

    我无法重现您的错误。该函数适用于我生成的示例数据集。

    # Sample data
    set.seed(2017);
    df <- as.data.frame(matrix(runif(200), ncol = 5));
    colnames(df) <- paste0("weight", seq(1:5));
    
    # Your function
    find_mean = function(...) {
      input_list = list(...)
      output_list = sapply(input_list,mean, na.rm=TRUE)
      return(output_list)
    }
    
    find_mean(df$weight1, df$weight2, df$weight3)
    #[1] 0.4736851 0.5569710 0.4300163
    

    您也可以在一行中实现相同的输出:

    sapply(c("weight1", "weight2", "weight3"), function(x) mean(df[, x]))
    #  weight1   weight2   weight3
    #0.4736851 0.5569710 0.4300163
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-09
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 2019-10-14
      相关资源
      最近更新 更多