我正在尝试在R中运行一个方差分析模型。我有一个包含3行和12列的数据文件。每一行都是解释变量特定水平的数据。第[i,j]个单元格是i水平j响应的值。该文件扩展名为".dat"。我正在使用以下R代码尝试获取一个36x2数据框,以代替原始的3x12数据框进行方差分析模型:
data <- read.table("usedcar.dat", row.names = 1)
young <- data[1,]
med <- data[2,]
old <- data[3,]
Price <- c(young, med, old)
Age <- as.factor(c(rep(1,12), rep(2,12), rep(3,12)))
data <- cbind(Age, Price)
data <- as.data.frame(data)
但是当我尝试从中获取ANOVA模型时,会出现无效的列表类型错误:
m1 <- aov(Price ~ Age, data = data)
Error in model.frame.default(formula = Price ~ Age, data = data, drop.unused.levels = TRUE) : invalid type (list) for variable 'Price'
我在这里做错了什么?
如果有帮助的话,这是一个随机矩阵:
replicate(12, rnorm(3))
这里是str(data)的结果:
str(data)
'data.frame': 36 obs. of 2 variables:
$ Age :List of 36
..$ 1 : int 1
..$ 2 : int 1
..$ 3 : int 1
...
..$ 36: int 3
$ Price:List of 36
..$ 1 : int 2300
...
..$ 36: int 2075
$
):aov(Price ~ Age, data = data)
。 - alistairestr(data)
;你的切割似乎把Price
留成了一个列表列。 - alistairecbind/as.data.frame
,请使用data <- data.frame(Age, Price)
。 - Rui Barradas