'Does `cor()` only work for numeric variables?

I'm interested in the correlation of x with y. x is an ordinal (Likert-type) variable. y is a continuous variable.

But when I use cor(x, y, method = "spearman") I get an error saying

'x' must be numeric`

Spearman's rho doesn't necessarily require x to be numeric. So I wonder how I can run this function?

set.seed(0)

x <- sample(c("None", "Little", "Often", "Always"), 20, replace = TRUE)
y <- round(runif(length(x), 100, 300))
data <- data.frame(subject=seq_len(length(x)), x, y)

cor(x, y, method = "spearman") # Error: 'x' must be numeric

#data:
   subject      x   y
1        1 Little 255
2        2   None 287
3        3 Always 142
4        4  Often 230
5        5   None 125
6        6 Little 153
7        7   None 177
8        8  Often 103
9        9  Often 176
10      10 Little 274
11      11 Little 168
12      12  Often 196
13      13  Often 220
14      14   None 199
15      15   None 137
16      16   None 265
17      17 Little 234
18      18 Little 259
19      19 Little 122
20      20 Little 245


Solution 1:[1]

You can recode the values:

data <- data %>% mutate(x2 = recode(x, "None" = 0, "Little" =1 , "Often"=2, "Always"=3))
cor(data$x2, data$y, method = "spearman")
[1] -0.1930743

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bloxx