invalid type (list) for variable

nbk picture nbk · Nov 5, 2017 · Viewed 63.9k times · Source

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

Answer

Ben Bolker picture Ben Bolker · Nov 5, 2017

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)