invalid type (list) for variable
Asked Answered
D

2

13

I am trying to run an anova model in R. I have a data file which contains 3 rows and 12 columns. Each row is data for a particular level of the explanatory variable. Cell [i,j] is the j'th response for level i. The file is ".dat" extension. I am running the following R code to try to get a 36 by 2 data frame to run the anova model instead of the 3 by 12 original data frame:

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)

But when I try to get the anova model out of it I get the invalid list type error:

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 am I doing wrong here?

Here's a random matrix if that will help:

replicate(12, rnorm(3))

Here is the str(data) result:

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
Diadromous answered 5/11, 2017 at 21:19 Comment(8)
Could you please include the exact error message?Tavi
It would also help to make your example reproducable, i.e. include a small dataset which generates the errorParamecium
If you're supplying a data parameter to a model, you shouldn't subset ($) in the formula: aov(Price ~ Age, data = data)Erk
same error with aov(Price~Age,data=data) :(Diadromous
Given the error, I'd look at str(data); your chopping seems to have left Price as a list column.Erk
Don't use cbind/as.data.frame, use data <- data.frame(Age, Price).Fortunetelling
@Rui If I use data<-data.frame(Age,Price) I get a 36X36 data frame...Diadromous
@Erk that does seem to be the problem, but why? also how do I correct?Diadromous
R
24

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)
Rotenone answered 5/11, 2017 at 21:39 Comment(2)
I think that the problem has something to do with the fact that I'm reading the data in as a data frame from a .dat file?Diadromous
unlist() may need to be applied to each column separately. When I applied it to the whole data frame, it turned the data frame into one long vector.Trackless
A
9

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.

Ataghan answered 21/4, 2018 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.