I have a data table with the results of an experiment that evaluated 2 factors: Light
and Day_light
at 2 different temperatures (Temperature
).
I performed a 2-way ANOVA for each Temperature
using the rstatix
package.
The result of the 2-way ANOVA is presented as a data table, with a column called SSn
. I would like to divide each SSn
value by the sum of all SSn
values for each Temperature
. For that, I used an approach similar to the one that the rstatix
package uses, but I was not successful. Below, I present the code I used and a brief graphic explanation of what I want to accomplish.
library(rstatix)
# Data frame
Temperature <- factor(c(rep("cold", times = 8),
rep("hot", times = 8)),
levels = c("cold", "hot"))
Light <- factor(rep(c(rep("blue", times = 4),
rep("yellow", times = 4)),
times = 2),
levels = c("blue", "yellow"))
Day_light <- factor(rep(c(rep("Day", times = 2),
rep("Night", times = 2)),
times = 4),
levels = c("Day", "Night"))
Result <- c(90.40, 85.20, 21.70, 25.30,
75.12, 77.36, 6.11, 10.8
85.14, 88.96, 30.21, 35.15)
Data <- data.frame(Temperature, Light, Day_light, Result)
# ANOVA
ANOVA <- Data %>%
group_by(Temperature) %>%
anova_test(Result ~ Light * Day_light,
detailed = TRUE)
ANOVA
# Calculations within the ANOVA data frame (not running)
Calculations <- ANOVA %>%
group_by(Temperature) %>%
ANOVA$SSn/sum(ANOVA$SSn)*100
Calculations
> ANOVA
# A tibble: 6 x 10
Temperature Effect SSn SSd DFn DFd F p `p<.05` ges
* <fct> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <dbl>
1 cold Light 354. 33.5 1 4 42.2 0.003 "*" 0.914
2 cold Day_light 8723. 33.5 1 4 1041. 0.0000055 "*" 0.996
3 cold Light:Day_light 6.07 33.5 1 4 0.725 0.442 "" 0.153
4 hot Light 773. 23.1 1 4 134. 0.000318 "*" 0.971
5 hot Day_light 5014. 23.1 1 4 869. 0.00000788 "*" 0.995
6 hot Light:Day_light 37.0 23.1 1 4 6.41 0.065 "" 0.616
I have already solved it partially, but I still don't know how to separate the calculation by Temperature
ANOVA$Calculations <- ANOVA$SSn/sum(ANOVA$SSn)*100
Graphical representation of my question
then...
I tend to stick to data.table personally, because it has some nice benefits, and there is quite a learning curve involved.
The traditional plyr way is also shown:
library(data.table)
ANOVA <- as.data.table(ANOVA)
ANOVA[, Calculations := SSn / sum(SSn) , by=Temperature ]
ANOVA
## and the plyr way:
ANOVA %>% group_by( Temperature ) %>%
mutate( Calculations = SSn / sum(SSn) )