提问人:vp_050 提问时间:4/2/2021 更新时间:4/2/2021 访问量:906
In 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
我试过了什么?
基于堆栈溢出中的讨论:如何将数据框列转换为数值类型?
transform(df, V2 = as.numeric(V2))
但是,在传递上述代码时,我收到以下错误(错误 2),即使在转换后也不断出现错误 1 消息。
Warning message:
In eval(substitute(list(...)), `_data`, parent.frame()) :
NAs introduced by coercion
答:
2赞
akrun
4/2/2021
#1
根据?cor.test
x, y - 数据值的数值向量。x 和 y 的长度必须相同。
一种选择是转换为并强制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
该代码给出警告并返回,因为它试图将类列直接转换为 。取而代之的是NA
character
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))
评论