0

I know that this question has been asked before but not in the context i am asking it now.

I have a dataframe that looks like this

      year    Units   
1 2005-2007   0.082 
2 2008-2010   0.411 
3 2011-2013   1.258 

my simple code looks like this

Year = mydata$year
Units = mydata$Units
cor.test(mydataRyear, mydata$Units, type=pearson)

when i run the code i get the error

Error in cor.test.default(mydata$year, mydata$Units, type = pearson) : 'x' must be a numeric vector

your comments and suggestions are highly welcomed

tom sawyer
  • 47
  • 1
  • 2
  • 9
  • 2
    `year` is not a numeric value - it will be either a character string or a factor. Check with `str(mydata)`. You could turn it into a numeric value by e.g. `as.numeric(substr(mydata$year,1,4))` or by using the factor levels `as.numeric(as.factor(mydata$year))` - although check the ordering makes sense if you use the latter. – Andrew Gustar Apr 19 '17 at 13:30
  • Thanks the second suggestion solved the error @AndrewGustar – tom sawyer Apr 19 '17 at 13:32

1 Answers1

2

year is not a numeric value - it will be either a character string or a factor. Check with str(mydata).

You could turn it into a numeric value by e.g. as.numeric(substr(mydata$year,1,4)) or by using the factor levels as.numeric(as.factor(mydata$year)) - although check the ordering makes sense if you use the latter.

Andrew Gustar
  • 17,295
  • 1
  • 22
  • 32