I have this data. Here example of it structure.
full_bajra_xts_red=structure(list(Date = structure(c(19144, 19145, 19146, 19147,
19148, 19149, 19150, 19151, 19152, 19153), class = "Date"), red = c(2127,
1302.9803048672, 1678.5, 2038, 1245.57566887554, 1982, 2088.5798414003,
1245.57566887554, 1834.12565893947, 2114)), row.names = c(NA,
10L), class = "data.frame")
full_bajra_xts_green=structure(list(Date = structure(c(19144, 19145, 19146, 19147,
19148, 19149, 19150, 19151, 19152, 19153), class = "Date"), green = c(1529,
1140.37413250597, 1225.04390857018, 1189.2683774732, 1418.5,
1542, 1380, 1376.86063429029, 1330.79379894137, 1494)), row.names = c(NA,
10L), class = "data.frame")
full_guar_xts_red=structure(list(Date = structure(c(19144, 19145, 19146, 19147,
19148, 19149, 19150, 19151, 19152, 19153), class = "Date"), red = c(2096,
1225.68256164161, 1641, 1333.91676380334, 1814.5, 2016, 1396,
2081, 2079.71725650448, 1193.11475693541)), row.names = c(NA,
10L), class = "data.frame")
full_guar_xts_green=structure(list(Date = structure(c(19144, 19145, 19146, 19147,
19148, 19149, 19150, 19151, 19152, 19153), class = "Date"), green = c(1516,
1071.73968116149, 1179, 1144.00588528805, 1498.06983978489, 1521,
1193.5, 1549, 1158.31497623564, 1296.86598194529)), row.names = c(NA,
10L), class = "data.frame")
full_arahis_xts_red=structure(list(Date = structure(c(19144, 19145, 19146, 19147,
19148, 19149, 19150, 19151, 19152, 19153), class = "Date"), red = c(2096,
1225.68256164161, 1641, 1333.91676380334, 1814.5, 2016, 1396,
2081, 2079.71725650448, 1193.11475693541)), row.names = c(NA,
10L), class = "data.frame")
full_arahis_xts_green <- data.frame(
Date = as.Date(c(19144, 19145, 19146, 19147, 19148, 19149, 19150, 19151, 19152, 19153), origin = "1970-01-01"),
green = c(1516, 1071.73968116149, 1179, 1144.00588528805, 1498.06983978489, 1521, 1193.5, 1549, 1158.31497623564, 1296.86598194529)
)
full_moong_xts_red=structure(list(Date = structure(c(19144, 19145, 19146, 19147,
19148, 19149, 19150, 19151, 19152, 19153), class = "Date"), red = c(2127,
1302.9803048672, 1678.5, 2038, 1245.57566887554, 1982, 2088.5798414003,
1245.57566887554, 1834.12565893947, 2114)), row.names = c(NA,
10L), class = "data.frame")
full_moong_xts_green= structure(list(Date = structure(c(19144, 19145, 19146, 19147,
19148, 19149, 19150, 19151, 19152, 19153), class = "Date"), green = c(1529,
1140.37413250597, 1225.04390857018, 1189.2683774732, 1418.5,
1542, 1380, 1376.86063429029, 1330.79379894137, 1494)), row.names = c(NA,
10L), class = "data.frame")
The data represents the name of the non-crop culture ("guar", "bajra", "moong", "arahis")
and contain the metric variables red
and green
. (For each variable its own separate dataset).
I need to compare each variable between pairs of cultures using the Wilcoxon t-test for paired samples.
guar vs bajra
guar vs moong
guar vs peanut
bajra vs moong
bajra vs arahis
for example, we take red
for the guar culture and red
for the bajra culture and compare them using the Wilcoxon T-test for paired samples and so on for all the culture according to the variables red
and green
.
This is how I do it (as a reproducible example I took only 2 variables, in fact there are many more)
cultures <- c("guar", "bajra", "moong", "arahis")
variables <- c("red", "green")
for (culture1 in cultures) {
for (culture2 in cultures) {
if (culture1 != culture2) {
for (var in variables) {
test_result <- wilcox.test(get(paste0("full_", culture1, "_xts_", var)), get(paste0("full_", culture2, "_xts_", var)), paired = TRUE)
print(paste("Comparison between", culture1, var, "and", culture2, var, ": p-value =", test_result$p.value))
}
}
}
}
and i get the error
Error in wilcox.test.default(get(paste0("full_", culture1, "_xts_", var)), :
'x' must be a number
what am I doing wrong, and how to correctly compare these variables between my pairs of cultures and how get desired output (numbers are p-values)
var guar_bajra guar_moong guar_arahis bajra_moong bajra_arahis moong_arahis
red 0.05 0.05 0.05 0.05 0.05 0.05
green 0.05 0.05 0.05 0.05 0.05 0.05
Thanks for your valuable help.
You're running wilcox.test
on the whole data frames and not just the "red" or "green" columns.
With minimal intervention as possible (and disregarding the edited note below), see if it works:
Edit. This code does not iterate "red-green" pairs, only "red-red" and "green-green": 24 of 56 possible combinations.
# for (culture1 in cultures) {
# for (culture2 in cultures) {
# if (culture1 != culture2) {
# for (var in variables) {
# test_result <- wilcox.test(
get(paste0("full_", culture1, "_xts_", var))[, var],
get(paste0("full_", culture2, "_xts_", var))[, var],
# paired = TRUE)
#
# print(paste("Comparison between", culture1, var, "and", culture2, var, ": p-value =", test_result$p.value))
# }
# }
# }
# }
Output:
[1] "Comparison between guar red and bajra red : p-value = 0.625"
[1] "Comparison between guar green and bajra green : p-value = 0.16015625"
[1] "Comparison between guar red and moong red : p-value = 0.625"
[1] "Comparison between guar green and moong green : p-value = 0.16015625"
[1] "Comparison between guar red and arahis red : p-value = NaN"
[1] "Comparison between guar green and arahis green : p-value = NaN"
[1] "Comparison between bajra red and guar red : p-value = 0.625"
[1] "Comparison between bajra green and guar green : p-value = 0.16015625"
[1] "Comparison between bajra red and moong red : p-value = NaN"
[1] "Comparison between bajra green and moong green : p-value = NaN"
[1] "Comparison between bajra red and arahis red : p-value = 0.625"
[1] "Comparison between bajra green and arahis green : p-value = 0.16015625"
[1] "Comparison between moong red and guar red : p-value = 0.625"
[1] "Comparison between moong green and guar green : p-value = 0.16015625"
[1] "Comparison between moong red and bajra red : p-value = NaN"
[1] "Comparison between moong green and bajra green : p-value = NaN"
[1] "Comparison between moong red and arahis red : p-value = 0.625"
[1] "Comparison between moong green and arahis green : p-value = 0.16015625"
[1] "Comparison between arahis red and guar red : p-value = NaN"
[1] "Comparison between arahis green and guar green : p-value = NaN"
[1] "Comparison between arahis red and bajra red : p-value = 0.625"
[1] "Comparison between arahis green and bajra green : p-value = 0.16015625"
[1] "Comparison between arahis red and moong red : p-value = 0.625"
[1] "Comparison between arahis green and moong green : p-value = 0.16015625"
Warning messages:
1: In wilcox.test.default(get(paste0("full_", culture1, "_xts_", var))[, :
cannot compute exact p-value with zeroes
2: In wilcox.test.default(get(paste0("full_", culture1, "_xts_", var))[, :
cannot compute exact p-value with zeroes
3: In wilcox.test.default(get(paste0("full_", culture1, "_xts_", var))[, :
cannot compute exact p-value with zeroes
4: In wilcox.test.default(get(paste0("full_", culture1, "_xts_", var))[, :
cannot compute exact p-value with zeroes
5: In wilcox.test.default(get(paste0("full_", culture1, "_xts_", var))[, :
cannot compute exact p-value with zeroes
6: In wilcox.test.default(get(paste0("full_", culture1, "_xts_", var))[, :
cannot compute exact p-value with zeroes
7: In wilcox.test.default(get(paste0("full_", culture1, "_xts_", var))[, :
cannot compute exact p-value with zeroes
8: In wilcox.test.default(get(paste0("full_", culture1, "_xts_", var))[, :
cannot compute exact p-value with zeroes