如何使用 R 在数据框中按因素分隔计算?
How to make calculations separated by factors within a data frame using R?
我有一个数据 table,其中包含评估 2 个因素的实验结果:Light
和 Day_light
在 2 个不同温度下 (Temperature
)。
我使用 rstatix
包对每个 Temperature
执行了 2 向方差分析。
2 向方差分析的结果显示为数据 table,其中有一列名为 SSn
。我想将每个 SSn
值除以每个 Temperature
的所有 SSn
值之和。为此,我使用了一种类似于 rstatix
包使用的方法,但我没有成功。下面,我展示了我使用的代码以及我想要完成的事情的简短图形说明。
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
我已经部分解决了,但我还是不知道如何通过Temperature
分开计算
ANOVA$Calculations <- ANOVA$SSn/sum(ANOVA$SSn)*100
我的问题的图形表示
然后...
我个人倾向于坚持使用 data.table,因为它有一些不错的好处,而且涉及相当大的学习曲线。
也展示了传统的plyr方式:
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) )
我有一个数据 table,其中包含评估 2 个因素的实验结果:Light
和 Day_light
在 2 个不同温度下 (Temperature
)。
我使用 rstatix
包对每个 Temperature
执行了 2 向方差分析。
2 向方差分析的结果显示为数据 table,其中有一列名为 SSn
。我想将每个 SSn
值除以每个 Temperature
的所有 SSn
值之和。为此,我使用了一种类似于 rstatix
包使用的方法,但我没有成功。下面,我展示了我使用的代码以及我想要完成的事情的简短图形说明。
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
我已经部分解决了,但我还是不知道如何通过Temperature
ANOVA$Calculations <- ANOVA$SSn/sum(ANOVA$SSn)*100
我的问题的图形表示
然后...
我个人倾向于坚持使用 data.table,因为它有一些不错的好处,而且涉及相当大的学习曲线。
也展示了传统的plyr方式:
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) )