Emma
Emma

Reputation: 1

as.numeric/as.integer not working in R

I'm new to R and trying to do a simple ANOVA. I created a data frame:

MeanTable <- data.frame(Age=c("2","2","2","4","4","4","6","6","6"), 
   Yield=c("12.812","15.17","13.868","24.456","23.444","25.312",
         "21.146","21.63","21.84"),stringsAsFactors = FALSE)

And then tried to do an ANOVA, but I got this warning message:

Warning messages: 1: In mean.default(X[[i]], ...) : argument is not numeric or logical: returning NA 2: In mean.default(X[[i]], ...) : argument is not numeric or logical: returning NA 3: In mean.default(X[[i]], ...) : argument is not numeric or logical: returning NA

So I assume that means I'm meant to use as.numeric to convert the values from factors into numbers. So I did this:

as.numeric(MeanTable$Age)
is.numeric(MeanTable$Age)

But got FALSE. What am I doing wrong? Or is my issue something else entirely?

Upvotes: 0

Views: 3219

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226871

You need to change the response value to numeric within the data frame.

MeanTable <- data.frame(Age=c("2","2","2","4","4","4","6","6","6"), 
   Yield=c("12.812","15.17","13.868","24.456","23.444","25.312",
         "21.146","21.63","21.84"),stringsAsFactors = FALSE)

MeanTable$Yield <- as.numeric(MeanTable$Yield)
lm(Yield~Age,data=MeanTable)

As @rawr points out it would be easier if your variables were numeric to begin with. However, be careful: if you make Age numeric rather than categorical you will be fitting a regression model rather than a 1-way ANOVA.

Upvotes: 3

Related Questions