I have a data.frame, which is similar to this one:
cb <- data.frame(group = ("A", "B", "C", "D", "E"), WC = runif(100, 0, 100), Ana = runif(100, 0, 100), Clo = runif(100, 0, 100))
Structure of the actual dataframe:
str(cb)
data.frame: 66936 obs of 89 variables:
$group: Factor w/ 5 levels "A", "B", "C" ...
$WC: int 19 28 35 92 10 23...
$Ana: num 17.2 48 35.4 84.2
$ Clo: num 37.2 12.1 45.4 38.9
....
mean <- colMeans(cb[,2:89])
mean
WC Ana Clo ...
52.45 37.23 50.12 ...
I want to perform One Sample t.tests on every group and every variable
For that I did the following:
A <- subset(cb, cb$group == "A")
B <- subset(cb, cb$group == "B")
...
t_A_WC <- t.test(A$WC, mu = mean[1], alternative = "two.sided")
t_B_WC <- t.test(B$WC, mu = mean[1], alternative = "two.sided")
....
t_A_Ana <- t.test(A$Ana, mu = mean[2], alternative = "two.sided")
t_B_Ana <- t.test(B$Ana, mu = mean[2], alternative = "two.sided")
....
t_A_Clo <- t.test(A$Clo, mu = mean[3], alternative = "two.sided")
t_B_Clo <- t.test(B$Clo, mu = mean[3], alternative = "two.sided")
....
The results are correct (or seem to be), but it is very time consuming typing the whole thing so many times.
Is there a smarter way to do that?
What I have tried:
From here
results <- lapply(mydf, t.test)
resultsmatrix <- do.call(cbind, results)
resultsmatrix[c("statistic","estimate","p.value"),]
But the results are somehow very wrong and does not fit to the values i calculated priorly.
EDIT:
Here is a link to a 10.000 row sample from the actual dataset