W.Cointreau - 11 months ago 62

R Question

My data frame is

`result`

It has the format like,

`org_A X Y Z`

130 44 55 66

312 33 62 77

.. .. .. ..

I want to use wilcox.test to compare org_A and X, and org_A and Y, then finally org_A and Z, and print its p-value.

so the result will look like

`X: 0.001932`

Y: 0.18271

Z: 0.001231

For that, I implemented like this,

`for (i in 1:ncol(result)){`

colnm <- colnames(result)[i]

wilcox.test(result$org_A, result$colnm, alternative=c("two.sided"), paired=FALSE)$p.value

}

but it warns with message

`Unknown column 'colnm'`

Even worse, it prints something but p values are same.

What should I do?

Answer Source

How about this?

```
result <- data.frame(org_A = c(130, 312, 123),
x = c(44, 33, 12),
y = c(55, 62, 43),
z = c(66, 77, 55))
answer <- rep(NA, (ncol(result) - 1))
for (i in 2:ncol(result)) {
answer[i-1] <- wilcox.test(result$org_A,
result[, i],
alternative = c("two.sided"),
paired = FALSE)$p.value
}
answer
[1] 0.1 0.1 0.1
```

*edit*

Here's another way of using `sapply`

.

```
sapply(result[, -1], FUN = function(x, result) {
wilcox.test(result$org_A,
x,
alternative = c("two.sided"),
paired = FALSE)$p.value
}, result = result)
```