2016-04-03 82 views
0

我是R新手,想要做一個簡單的ANOVA。我創建的數據幀:as.numeric/as.integer不工作在R

MeanTable <- data.frame(Age=c("2","2","2","4","4","4","6","6","6"), 
    Yield=c("12.812","15.17","13.868","24.456","23.444","25.312", 
     "21.146","21.63","21.84"),stringsAsFactors = FALSE) 

然後試圖做的ANOVA,但我得到這個警告消息:

警告消息: 1:在mean.default(X [[I] ],...): 參數不是數字或邏輯:返回NA 2:在mean.default(X [[i]],...)中: 參數不是數字或邏輯:返回NA 3:在mean.default(X [[i]],...)中: 參數不是數字或邏輯:返回NA

所以我假設我的意思是使用as.numeric將因素中的值轉換爲數字。所以我這樣做:

as.numeric(MeanTable$Age) 
is.numeric(MeanTable$Age) 

但得到FALSE。我究竟做錯了什麼?或者我的問題完全是其他問題?

+2

你爲什麼要把所有東西都加上引號?或者數據是如何來到你身上的?爲了蠻力,你可以添加'MeanTable [] < - lapply(MeanTable,type.convert)',但這可以通過不做引號來避免 – rawr

回答

2

您需要將響應值更改爲數字框內的數字

MeanTable <- data.frame(Age=c("2","2","2","4","4","4","6","6","6"), 
    Yield=c("12.812","15.17","13.868","24.456","23.444","25.312", 
     "21.146","21.63","21.84"),stringsAsFactors = FALSE) 

MeanTable$Yield <- as.numeric(MeanTable$Yield) 
lm(Yield~Age,data=MeanTable) 

@rawr指出,如果您的變量是數字開頭的話,會更容易。但是,請注意:如果您製作的數字是Age而不是分類,那麼您將使用迴歸模型而不是單向ANOVA。