我有两个 CSV 文件,其中包含如下数据:
3,5,1,4,3,5
我正在尝试进行 Mann-Whitney U 测试,但是当我将两个 CSV 文件读入变量“group1”和“group2”时,我收到消息:
> # Performing the Mann-Whitney U test
> test_result <- wilcox.test(group1, group2, exact = FALSE)
Error in wilcox.test.default(group1, group2, exact = FALSE) :
'x' must be numeric
直接在 R 脚本中读取数据效果很好,如下所示:
group1 <- c(3, 5, 1, 4, 3, 5)
group2 <- c(4, 9, 6, 2, 1,9)
所以,我的问题是如何从 CSV 文件导入数据并使变量与上面直接使用“c”符号相同(这样 Mann-Whitney 就可以工作)?
这是我的 R 脚本:
# Example data for two groups
group1 <- read.csv("/Users/..../group1.csv", header = FALSE, colClasses = "integer", sep=",")
group2 <- read.csv("/Users/..../group2.csv", header = FALSE, colClasses = "integer", sep=",")
#group1 <- c(3, 5, 1, 4, 3, 5)
#group2 <- c(4, 9, 6, 2, 1,9)
# Performing the Mann-Whitney U test
test_result <- wilcox.test(group1, group2, exact = FALSE)
# Displaying the test results
print(test_result)
# To specifically display the U statistic, p-value, and related ranks, you can access them as follows:
cat("U statistic:", test_result$statistic, "\n")
cat("P-value:", test_result$p.value, "\n")
# For ranks, since the Mann-Whitney U test in R does not directly return ranks of the two samples,
# you can compute them separately if needed using the rank function on the combined data
combined_data <- c(group1, group2)
ranks <- rank(combined_data)
cat("Ranks:", ranks, "\n")
感谢您提供如何解决此问题的所有建议。
您可能只需将数据转换为数字,例如
group1 <- as.numeric(group1)
group2 <- as.numeric(group2)
正如 @robert-hacken 善意添加的,您应该首先调查一下为什么获得字符串值。