I have data points for two groups arranged in the same row, and multiple rows of data (400k+). I would like to compare the variance of two groups for each of these 400K+ rows. The data would be something like the following -
y<-rbind(c(1,2,20,50,100,1,2,3,1,2),c(20,2,80,50,100,1,2,3,1,2))
group<-structure(c(1L,1L,1L,1L,1L,2L,2L,2L,2L,2L), .Label = c("T","C"), class="factor")
I can run the leveneTest
from the car
package on a single row of data, for example -
leveneTest(y = y[1,], group = group) # first row of data
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 1 4.527 0.06603 .
8
or
leveneTest (y = y[2,], group = group) # second row of data
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 1 11.92 0.008662 **
8
But obviously this would be impractical for 400k+ rows of data.
I thought it would be something simple, like using the apply
as I would for the t.test
, for example -
apply(y, 1, function (x) t.test(x[1:5],x[6:10])$p.value)
[1] 0.15260837 0.05551746
But when I try it for the leveneTest
apply(y, 1, function(x) leveneTest (y = y, group = group))
I get the following error
Error in complete.cases(y, group) :
not all arguments have the same length
Does any one know how to do this?
As we are using the anonymous function call, the 'y' for leveneTest
is 'x' (i.e. values in each row) and not the full dataset.
apply(y, 1, function(x) leveneTest (y = x, group = group))
Or instead of using the anonymous call, the following should also work
apply(y, 1, FUN = leveneTest, group=group)