My data and code are like this:
my_vector <- rnorm(150)
my_factor1 <- gl(3,50)
my_factor2 <- gl(2,75)
tapply(my_vector, my_factor1, function(x)
t.test(my_vector~my_factor2, paired=T))
I want to do a separate t-test for each level of my_factor1, to test my_vector for both levels of my_factor2.
However, with my code the t-test is not splitting the levels of my_factor1, and the results are equal for each level because my_vector is entirely included in each t.test.
This is the output of my code:
$`1`
Paired t-test
data: my_vector by my_factor2
t = 0.2448, df = 74, p-value = 0.8073
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2866512 0.3669667
sample estimates:
mean of the differences
0.04015775
$`2`
Paired t-test
data: my_vector by my_factor2
t = 0.2448, df = 74, p-value = 0.8073
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2866512 0.3669667
sample estimates:
mean of the differences
0.04015775
$`3`
Paired t-test
data: my_vector by my_factor2
t = 0.2448, df = 74, p-value = 0.8073
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.2866512 0.3669667
sample estimates:
mean of the differences
0.04015775
What am I missing or doing wrong?
function(x)
but where is x in the formula? and also what is~
in the function? – user227710lapply(X = split(my_vector, my_factor1), FUN = function(z) {lapply(X = split(my_vector, my_factor2), FUN = function(y, z, ...) t.test(y, z), z, paired = T)})
This does what you want in a generic way and can deal with1:n
factors in factor vector, but it does not usetapply
– Vlo