2017-02-07 100 views
-2

數據幀上我有一個DF這樣創建的:錯誤計算平均中的R

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的數據框上的平均計算有一些差異,但我不確定我在這裏做錯了什麼。

回答

0

您的列存儲爲因子。以下編輯到您的代碼應該工作

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