I have data frame that looks like this:
set.seed(123)
df <- data.frame(factor1 = rep(c("A", "B"),50),
factor2 = rep(c("X","X", "Y", "Y"),25),
value = rnorm(100))
I want to calculate some summary values for factor1:factor2 pairs. I calculated the means and sd using:
summary <- as.matrix(cast(df, factor1~factor2, mean))
summary.sd <- as.matrix(cast(df, factor1~factor2, sd))
summary.table <- t(rbind(summary, summary.sd))
colnames(summary.table) <- c("A.mean", "B.mean", "A.sd", "B.sd")
But I would like to add to summary.table the p-value from a t.test comparing A vs B. So far I have done this, but not only does this not write to summary.table but i can’t get the names of the factor2 variable to print out along with it:
for (measurement in levels(df$factor2)) print(t.test(value~factor1, data=subset(df, factor2==measurement)))
I figure there must be some simple way to do this, or maybe a package that I am not aware of that would make this much more straightforward.
I’d do it this way:
First, get
meanandsdsummaries usingddplyfromplyrusingsummariseThen, get
p-valuesfort-testwith NULLmean(A) = mean(B)for bothXandYlevels infactor2:Then, using
reshape2‘smeltanddcastcasto1to the desired format.Now, merge it with
o2: