Reputation: 11772
I am trying to calculate a p-value for some specific columns in a data frame.
df <- data.frame(
expa_1=sample(c(rnorm(40,5,1) + 30)),
expa_2=sample(c(rnorm(40,5,1) + 30)),
expa_3=sample(c(rnorm(40,5,1) + 30)),
expb_1=sample(c(rnorm(40,15,1) + 30)),
expb_2=sample(c(rnorm(40,15,1) + 30)),
expb_3=sample(c(rnorm(40,15,1) + 30)))
apply(df,1,function(x) t.test(x[1:3],x[4:6])$p.value)
So this is my little example which seems to work fine... If I have more than just the columns I want to calculate the p-value from, then I get an error.
df <- data.frame(names=c(letters,LETTERS[1:14]),
expa_1=sample(c(rnorm(40,5,1) + 30)),
expa_2=sample(c(rnorm(40,5,1) + 30)),
expa_3=sample(c(rnorm(40,5,1) + 30)),
expb_1=sample(c(rnorm(40,15,1) + 30)),
expb_2=sample(c(rnorm(40,15,1) + 30)),
expb_3=sample(c(rnorm(40,15,1) + 30)))
apply(df,1,function(x) t.test(x[2:4],x[5:7])$p.value)
Can somebody explain this to me and maybe give me a better solution for this?
Thanks in advance
Upvotes: 3
Views: 659
Reputation: 887831
You could try
apply(df[-1], 1, function(x) t.test(x[1:3], x[4:6])$p.value)
The first column is character/factor. When you do apply
, it converts to matrix
and the values will be converted to 'character' as 'matrix' can hold only one class.
For example
apply(df,1,function(x) x[2:4])[1:3]
#[1] "34.33018" "34.93256" "34.74045"
Upvotes: 4