在 eval(substitute(list(...)), `_data`, parent.frame()) 中:由强制引入的 NA

In eval(substitute(list(...)), `_data`, parent.frame()) : NAs introduced by coercion

我想找到这些变量之间的 Spearman 秩相关 rho 值。

V1  V2  V3  V4
A   SUV Yes Good
A   SUV No  Good
B   SUV No  Good
B   SUV Yes Satisfactory
C   car Yes Excellent
C   SUV No  Poor
D   SUV Yes Poor
D   van Yes Satisfactory
E   car No  Excellent


corr <- cor.test(x=df$V2, y=df$V3, method = "spearman")
corr

传递代码时,我收到以下错误(错误 1)

Error in cor.test.default(x = df$V2, y = df$V3, method = "spearman") : 
  'x' must be a numeric vector

我尝试了什么?

基于堆栈溢出中的讨论:How to convert a data frame column to numeric type?

transform(df, V2 = as.numeric(V2))

但是,在通过上述代码时,我收到以下错误(错误 2),并且即使在转换后错误 1 ​​消息仍然出现。

Warning message:
In eval(substitute(list(...)), `_data`, parent.frame()) :
  NAs introduced by coercion

根据?cor.test

x, y - numeric vectors of data values. x and y must have the same length.

一种选择是转换为 factor 并强制转换为 integer

cor.test(x=as.integer(factor(df$V2)), y=as.integer(factor(df$V3)), method = "spearman")

    Spearman's rank correlation rho

data:  as.integer(factor(df$V2)) and as.integer(factor(df$V3))
S = 95.158, p-value = 0.593
alternative hypothesis: true rho is not equal to 0
sample estimates:
      rho 
0.2070197 

该代码给出了警告和 return NA,因为它试图将 character class 列直接转换为 numeric。取而代之的是 factor -> numeric/integer

transform(df, V2 = as.numeric(factor(V2)))

数据

df <- structure(list(V1 = c("A", "A", "B", "B", "C", "C", "D", "D", 
"E"), V2 = c("SUV", "SUV", "SUV", "SUV", "car", "SUV", "SUV", 
"van", "car"), V3 = c("Yes", "No", "No", "Yes", "Yes", "No", 
"Yes", "Yes", "No"), V4 = c("Good", "Good", "Good", "Satisfactory", 
"Excellent", "Poor", "Poor", "Satisfactory", "Excellent")), 
class = "data.frame", row.names = c(NA, 
-9L))