How do you overcome this?
Two ways:
- Feed the data to
cor()
how the function expects you to:
data(Titanic)
Titanic <- data.frame(Titanic)
cor(Titanic$Sex, Titanic$Freq) # Bad, Titanic$Sex is a factor, not numeric
# Error in cor(Titanic$Sex, Titanic$Freq) : 'x' must be numeric
cor(as.numeric(Titanic$Sex), Titanic$Freq) # Good, cor() expects numeric
# [1] -0.294397
If you don't want to have to type out as.numeric
, you can just use c()
:
cor(c(Titanic$Sex), Titanic$Freq)
# [1] -0.294397
- If you don't want to have to do that all the time, you can just make your own
cor()
to do it for you:
cor <- function(x, y, ...) {
if ( !is.numeric(x) ) {
message("Converting x to numeric.")
x <- as.numeric(x)
}
if ( !is.numeric(y) ) {
message("Converting y to numeric.")
y <- as.numeric(y)
}
return(stats::cor(x, y, ...))
}
data(Titanic)
Titanic <- data.frame(Titanic)
cor(Titanic$Sex, Titanic$Freq)
# Converting x to numeric.
# [1] -0.294397
Why won't R do things like SPSS?
- It's different software. You may have built up certain assumptions or expectations working with one particular piece of software for some time, but you should lose the expectation that other software will, or should, work the same way.
- R's way may be more appropriate. You can see some discussion in PoGibas's comment, and on Cross Validated on here.