[R] Using perm.t.test() upon Matrix/Dataframe columns parted by factor instead of t.test()
Gunnar Oehmichen
oehm8895 at uni-landau.de
Sat Jul 30 10:45:04 CEST 2011
Just checked. To get lapply to work for the both functions, I have to
convert the matrix M into a dataframe. Trying it with apply for the
matrix works perfectly fine. The missing x was the problem
So here my improved code.
require(Deducer) #Package for perm.t.test
c(rep("A", 5), rep("B", 5))->Faktor
matrix(rnorm(100, mean=20, sd=4), nrow=10, ncol=10)->M
colnames(M) <- c("species1","species2",
"species3","species4","species5","species6","species7","species8","species9",
"species10")
###Conventional T-Test to test for differences of each species per factor
as.data.frame(M)->M2
apply(
M, 2, function(x)
t.test(x~ Faktor)
)
lapply(
M2, function(x)
t.test(x~ Faktor)
)
#Both versions ork fine, either apply for the matrix or lapply for the
data-frame
###Trying it for perm.t.test without the helpful formular expression
lapply(
M2, function(x)
perm.t.test(subset(x, Faktor=="A"),
subset(x, Faktor=="B")))
#For the perm.t.test lapply works with a dataframe
apply(
M, 2, function(x)
perm.t.test(subset(x, Faktor=="A"),
subset(x, Faktor=="B")))
Thank you very much for your help,
Gunnar
>
>
>
>> Just a hunch I can't test from my phone, but in your final lapply you
>> are passing a function of x that has no x in it, so I wouldn't be
>> surprised if R was unhappy about that.
>>
>> Change the latter M's to x and see if that helps.
>
> Not likely. 'x' is a formal argument of the function. He's using
> lapply on a matrix. lapply is generally used for lists. but in this
> case it results in sending individual numbers one-by-one to that
> function, They are them each being t.tested against a ten item vector.
> Failure is the predictable outcome but he did not see fit to reproduce
> the informative error message that told him there was a mismatch of
> lengths.
>
> He should use:
> apply(M, 2, function(x)
> t.test(x~ Faktor))
More information about the R-help
mailing list