Getting the means for your experimental conditions

Make sure you have dplyr installed and loaded into your library for this step.

# create a new table called "abr"
# that groups results based on condition and result, our two IV
abr = dta%>%group_by(Condition, Result)%>%summarize(
# create a column called M that gives me the mean of my Abr variable 
#(from the dataset) and that ignores missing data (na.rm = TRUE)
  M = mean(Abr, na.rm = TRUE), 
# do the same for standard deviation 
  SD = sd(Abr, na.rm = TRUE),
# tell me the sample size of each cell 
  n=n()
)
# calculate the error on the estimate of the mean and add it as a column. 
# we need this term for plotitng 
abr$error = 1.96*(abr$SD / sqrt(abr$n)) 
# print this table in the output 
print(abr)

Words in pink are variables you can re-name in this code to whatever you want

Words in green are files and variables specific to your existing R environment that should already exist (i.e., they might vary from my code to yours, but you are not creating them in this code)

Words in black are non-negotiable parts of the code

Term What it’s doing
abr = Create a new table called “abr”
I highly encourage you to choose a new name for each table you make. It prevents us from saving-over important data and allows us to notice errors in the code much faster.
dta%>% working within my data file called “dta” (replace this with the name of your data file)
group_by(Condition, Result)%>% Report all results grouped by the different levels of my IVS
Condition and Result are the names of the IV variables in my dataset
summarize( summarize the data you have using the following summary statistics
M = Create a new column in the table called “M” (where we will put the means)
mean(Abr, na.rm = TRUE), Create the mean of this variable in my dataset called Abr (change to your variable name)
na.rm = TRUE: ignore missing cases (otherwise it will return a mean of NA)
SD = sd(Abr, na.rm = TRUE), Same as we did for the means but for standard deviation
n=n()) Create a new column in the table with the n of each cell in the 2x2 (or whatever you have)
abr$error Create a new column called “error” in the table we previously called “abr”
1.96*(abr$SD / sqrt(abr$n)) This step is necessary if you want to create a graph with error bars, as we call this variable in the plotting code below.
For context, the equation for standard error is 1.96*(SD/sqrt(n))
abr$SD = Calling the “SD” column of the abr table
sqrt(abr$n) = Getting the square root of the n column in the abr table
print(abr) Print the new table so we can see our results!

Printing a graph

This can be a really nice block to set echo=FALSE

p1 = ggplot(abr, aes(x=Condition, y=M, fill=Result)) + 
  geom_bar(position=position_dodge(), stat="identity", color="black")+
  scale_fill_manual(values=c("white","gray87", "gray43"))+
  geom_errorbar(aes(ymin=M-error, ymax=M+error), width=.2,
                 position=position_dodge(.9))+ 
  ylab("Brand Attitude") + xlab("Condition")+ #re-label the axes
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), #remove background 
        panel.background = element_blank(), axis.line = element_line(colour = "black"), legend.text = element_text(size = 14), axis.text = element_text(size = 14), axis.title = element_text(size = 20), legend.title = element_text(size = 20))
print(p1)

Is there a difference between experimental conditions?

T-test

t.test(Abr ~ Result, data = dta)

One-Way ANOVA

m1 = aov(Abr ~ Condition, data = dta) # run the ANOVA
summary(m1) # print the ANOVA results 
emmeans(m1, pairwise~Condition)$contrasts #run post hoc tests if necessary

Factorial ANOVA

m1 = aov(Abr ~ Condition*Result, data = dta) # run the ANOVA
summary(m1) # print the ANOVA results 
emmeans(m1, pairwise~Result|Condition)$contrasts # run the simple effects one way 
emmeans(m1, pairwise~Condition|Result)$contrasts# run the simple effects the other way