invalid type (list) for variable
The error message invalid type (list) for variable x
from the lm
or other formula-based functions generally indicates that variable x
is expecting a vector, but instead is a list. A standard model for debugging the error is to examine the result of str(data_frame_name$x)
(where data_frame_name
is the data frame that contains x). Usually, you would find that x
is not quite the data type that you expect.
tl;dr rows of data frames are lists, not numeric vectors. When you read.table()
you get a data frame (so constructing a matrix, as I did before, doesn't replicate the problem).
data <- as.data.frame(matrix(rnorm(36),nrow=3))
young <- data[1,]; med <- data[2,]; old <- data[3,]
Price <- c(young, med, old)
str(Price)
## ## List of 36
## ## $ V1 : num 0.648
## ## $ V2 : num 0.157
## ## ...
The fact that this is a list, not a numeric vector, is a problem.
There are a variety of ways of handling this. The easiest is unlist()
:
dd <- data.frame(Age,Price=unlist(Price))
aov(Price~Age,dd)