计算R中数据帧的均值时出错

时间:2017-02-07 23:53:09

标签: r ggplot2 plyr

我有一个这样的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到数据帧的平均计算存在一些差异,但我不确定我在这里做错了什么。

1 个答案:

答案 0 :(得分:0)

您的列存储为因子。以下对代码的编辑应该有效

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