. Advertisement .
..3..
. Advertisement .
..4..
As advised, I used some code samples in another forum, but it could not improve the problem. My question is the “invalid type (list) for variable” in r – How to solve it? The command line is:
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)
replicate(12, rnorm(3))
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
and the result:
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'
What does the message mean? Can you advise me to fix it? If you have other better answers, leave them in the answer box below.
The cause: Because data frames’ default rows are lists, but not numeric vectors. When you read
.table()
, you obtain a data frame (Therefore, creating a matrix won’t change anything).Solution: The issue is that this is a list rather than a numeric vector. So,
Unlist()
is a simple technique to deal with this:invalid type (list) for variable x
is an error message thatlm
and other formula-based functions give. It indicates thatx
was not expecting a vector but instead a list. Thestr(data_frame_name$x)
result is a standard way to debug the error.data_frame_name
is the data frame containing x. You will usually findx
not the data type you expected.