Given a matrix m how can I do a t.test on the rows/variable (testing if mean is different from zero) and get a matrix where each column corresponds to e.g. the t.test$statistic and t.test$p.value for the rows. Since some rows have several NAs I at the same time want to make sure the t.test doesnt fail from this; thus in this case the row of the resulting matrix would be NA in both the t.test$statistic and t.test$p.value columns. I thought of something like whats shown below but I cannot get it right. In the end I need to do this on a list of matrices but figure that once I can do it on a single matrix I can use lapply on the list of matrices. Thanks!
res <- apply(m, 1, function(x) {
u <- matrix(NA, nrow = nrow(m), ncol = 4, dimnames = list(
c(rownames(m)),
c("Stats", "P-values")
))
if(sum(!is.na(x)) > 1)
u[,1] <- t.test(x)$statistic
u[,2] <- t.test(x)$p.value
else NA
return(u)
}
)