代码之家  ›  专栏  ›  技术社区  ›  hpesoj626

计算二阶阶乘表的平均向量

  •  0
  • hpesoj626  · 技术社区  · 8 年前

    我试图计算变量的平均试剂向量 RBC , WBC hemoglobin 。我对R比较陌生,所以我的问题是:你能告诉我用R进行以下计算的更简单的方法吗?数据来自 Rencher 。我正在尝试按照伦彻的例子练习用R进行计算。

    reagent.dat <- read.table("https://dl.dropboxusercontent.com/u/28713619/reagent.dat")
    colnames(reagent.dat) <- c("reagent", "subject", "RBC", "WBC", "hemoglobin")
    reagent.dat$reagent <- factor(reagent.dat$reagent)
    reagent.dat$subject <- factor(reagent.dat$subject)
    library(plyr) 
    library(dplyr)
    library(reshape2)
    # Calculate the means per variable, across reagents
    reagent.datm <- melt(reagent.dat)
    group.means <- ddply(reagent.datm, c("variable","reagent"), summarise,mean=mean(value))
    group.means <- tbl_df(group.means)
    newdata <- group.means %>% select(reagent, mean)
    # Store the group means into a matrix
    y_bar <- matrix(c(rep(NA, times=12)), ncol=4)
    for (i in 1:4)
      y_bar[,i] <- as.matrix(filter(newdata, reagent == i)$mean, ncol=1)
    y_bar
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   Michael Gao    8 年前

    这个 dplyr 这个包实际上可以很容易地简化您的代码,并且绝对值得学习,因为它的功能非常强大。例如:

    reagent.dat <- read.table("https://dl.dropboxusercontent.com/u/28713619/reagent.dat")
    colnames(reagent.dat) <- c("reagent", "subject", "RBC", "WBC", "hemoglobin")
    
    #Using dplyr
    library(dplyr)
    reagentmeans <- reagent.dat  %>% select(reagent, RBC, WBC, hemoglobin)  %>% 
    group_by(reagent)  %>% 
    summarize(mean_RBC = mean(RBC), mean_WBC = mean(WBC),
     mean_hemoglobin = mean(hemoglobin))
    
    > reagentmeans
    Source: local data frame [4 x 4]
    
      reagent mean_RBC mean_WBC mean_hemoglobin
       (fctr)    (dbl)    (dbl)           (dbl)
    1       1    7.290   4.9535          15.310
    2       2    7.210   4.8985          15.725
    3       3    7.055   4.8810          15.595
    4       4    7.025   4.8915          15.765
    
        2
  •  2
  •   Sotos    8 年前

    你可以使用 data.table ,

    library(data.table)
    setDT(reagent.dat)[, lapply(.SD, mean), by = reagent, .SDcols = c('RBC', 'WBC', 'hemoglobin')]
    #   reagent   RBC    WBC hemoglobin
    #1:       1 7.290 4.9535     15.310
    #2:       2 7.210 4.8985     15.725
    #3:       3 7.055 4.8810     15.595
    #4:       4 7.025 4.8915     15.765