【问题标题】:Error computing mean on data frame in RR中数据帧的错误计算平均值
【发布时间】:2017-02-07 23:53:08
【问题描述】:

我有一个这样创建的 DF:

    Sample Concentration
1  Dp10 WT        121.36
2  Dp10 WT        129.11
3  Dp10 WT        149.46
4     Dp10         141.3
5     Dp10        129.11
6     Dp10        131.02
7  Dp16 WT             0
8  Dp16 WT         134.8
9  Dp16 WT         144.5
10    Dp16        134.33
11    Dp16        129.11
12    Dp16        160.02

A = matrix(
c("Dp10 WT", "Dp10 WT", "Dp10 WT",
  "Dp10", "Dp10", "Dp10",
  "Dp16 WT", "Dp16 WT", "Dp16 WT",
  "Dp16", "Dp16", "Dp16",
  121.36, 129.11, 149.46, 141.3, 129.11, 131.02,
  0, 134.8, 144.5, 134.33, 129.11, 160.02),
nrow=12,
ncol=2,
byrow = FALSE)

dimnames(A) = list(seq(1,12)
,c('Sample', 'Concentration')) # column names
DF=data.frame(A)

但计算如下所示的平均值会给我以下错误。

mm <- ddply(DF, "Sample", summarise, conc = mean(Concentration, na.rm=TRUE))

Error in attributes(out) <- attributes(col) : 
'names' attribute [12] must be the same length as the vector [3]

我知道从 R 3.0 开始,数据帧的均值计算存在一些差异,但我不确定我在这里做错了什么。

【问题讨论】:

    标签: r ggplot2 plyr


    【解决方案1】:

    您的列被存储为因子。对您的代码进行以下编辑应该可以工作

    DF=data.frame(A,stringsAsFactors = FALSE)
    DF$Concentration = as.numeric(DF$Concentration)
    mm <- ddply(DF, "Sample", summarise, conc = mean(Concentration, na.rm=TRUE))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-23
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 2022-01-06
      • 2019-07-19
      • 2021-04-30
      相关资源
      最近更新 更多