英文:
Exporting the results of TukeyHSD test with basic descriptive statistics by factor group to excel
问题
我想要将TukeyHSD测试的结果与基本描述性统计数据按分组因子导出到Excel文件。我有一个包含16个水平的因子的数据框:
main.data <- runif(256)
main.factor <- c("1st control", "2nd control", "Control A", "Control B",
"Variant A", "Variant B", "Variant C",
"1st Variant", "2nd Variant", "3rd Variant",
"Treatment 1", "Treatment 2", "Treatment 3",
"1st Treatment", "2nd Treatment", "3rd Treatment")
df <- data.frame(experiment=rep(main.factor, each=16), Main.value=main.data)
df$experiment <- factor(df$experiment, levels = c("1st control", "2nd control", "Control A", "Control B",
"Variant A", "Variant B", "Variant C",
"1st Variant", "2nd Variant", "3rd Variant",
"Treatment 1", "Treatment 2", "Treatment 3",
"1st Treatment", "2nd Treatment", "3rd Treatment"))
我正在使用单因素方差分析(ANOVA),然后进行TukeyHSD测试:
model.1 <- lm(Main.value ~ experiment, data = df)
anova(model.1)
library(agricolae)
result <- HSD.test(model.1, "experiment", group = F)
我想要导出TukeyHSD测试的结果和基本描述性统计数据按分组因子。我尝试过以下代码:
result.1 <- result$comparison
library("xlsx")
write.xlsx(result.1, file = "main.result.xlsx", col.names = TRUE, row.names = TRUE, sheetName = "TukeyHSD_test")
主要问题是比较结果以字母顺序排列,但我想使用以下顺序:
factor.order <- c("1st control", "2nd control", "Control A", "Control B", "Variant A", "Variant B", "Variant C", "1st Variant", "2nd Variant", "3rd Variant", "Treatment 1", "Treatment 2", "Treatment 3", "1st Treatment", "2nd Treatment", "3rd Treatment")
是否可以更改排序?是否可以导出结果到Excel时添加空行?我想要在第一个因子水平与其他因子水平进行比较时添加空行(第一个空行),第二个因子水平与其他进行比较时添加空行(第二个空行),第三个因子水平与其他进行比较时添加空行(第三个空行)等。
当我导出按分组因子的描述性统计结果时,它也按字母顺序排列。如何更改?
result.2 <- result$mean
write.xlsx(result.2, file = "main.result.xlsx", col.names = TRUE, row.names = TRUE,
sheetName = "descriptive_statistics", append = TRUE)
是否有人可以帮助我?非常感谢。
英文:
I would like to export the results of TukeyHSD test with basic descriptive statistics by grouping factor to excel file. I have a data frame with factor containing 16 levels:
main.data <- runif(256)
main.factor <- c("1st control", "2nd control", "Control A", "Control B",
"Variant A", "Variant B", "Variant C",
"1st Variant", "2nd Variant", "3rd Variant",
"Treatment 1", "Treatment 2", "Treatment 3",
"1st Treatment", "2nd Treatment", "3rd Treatment")
df <- data.frame(experiment=rep(main.factor,each=16), Main.value=main.data)
df$experiment <- factor(df$experiment, levels = c("1st control", "2nd control", "Control A", "Control B",
"Variant A", "Variant B", "Variant C",
"1st Variant", "2nd Variant", "3rd Variant",
"Treatment 1", "Treatment 2", "Treatment 3",
"1st Treatment", "2nd Treatment", "3rd Treatment"))
I am using one-way ANOVA and after TukeyHSD test:
model.1 <- lm(Main.value~experiment, data=df)
anova(model.1)
library(agricolae)
result <- HSD.test(model.1, "experiment", group=F); result
I would like to export results of TukeyHSD test and basic descriptive statistics by grouping factor. I have tried this:
result.1 <- result$comparison
library("xlsx")
write.xlsx(result.1, file = "main.result.xlsx", col.names = TRUE, row.names = TRUE, sheetName = "TukeyHSD_test")
The main problem is that comparison is in alphabetical order, but I want to use this order:
factor.order <- c("1st control", "2nd control", "Control A", "Control B", "Variant A", "Variant B", "Variant C", "1st Variant", "2nd Variant", "3rd Variant", "Treatment 1", "Treatment 2", "Treatment 3", "1st Treatment", "2nd Treatment", "3rd Treatment")
It is possible to change? It can be also export result to excel with empty row? I would like to add empty row when the first factor level was compaired to other factor levels (first empty row), the second factor level was compaired to others (second empty row), third factor level was compaired to other (third empty row) etc.
When I export the result of descriptive statistics by grouping factor, it is also in alphabetical order. How can I change?
result.2 <- result$mean
write.xlsx(result.2, file = "main.result.xlsx", col.names = TRUE, row.names = TRUE,
sheetName = "descriptive_statistics", append = TRUE)
Can somebody help me? Thank You very much.
答案1
得分: 1
Here is the translated code:
library(agricolae)
library(tidyverse)
library(xlsx)
main.data <- runif(256)
main.factor <- c("1st control", "2nd control", "Control A", "Control B",
"Variant A", "Variant B", "Variant C",
"1st Variant", "2nd Variant", "3rd Variant",
"Treatment 1", "Treatment 2", "Treatment 3",
"1st Treatment", "2nd Treatment", "3rd Treatment")
df <- data.frame(experiment=rep(main.factor, each=16), Main.value=main.data)
df$experiment <- factor(df$experiment, levels = c("1st control", "2nd control", "Control A", "Control B",
"Variant A", "Variant B", "Variant C",
"1st Variant", "2nd Variant", "3rd Variant",
"Treatment 1", "Treatment 2", "Treatment 3",
"1st Treatment", "2nd Treatment", "3rd Treatment"))
factor.order <- c("1st control", "2nd control", "Control A", "Control B", "Variant A", "Variant B", "Variant C", "1st Variant", "2nd Variant", "3rd Variant", "Treatment 1", "Treatment 2", "Treatment 3", "1st Treatment", "2nd Treatment", "3rd Treatment")
model.1 <- lm(Main.value~experiment, data=df)
result <- HSD.test(model.1, "experiment", group=F)
result.1 <- result$comparison
result.2 <- result$mean
result_1_sheet = result.1 %>%
rownames_to_column("grouping_factor") %>%
separate(grouping_factor, into = c("factor_a", "factor_b"), sep = " - ") %>%
mutate(
factor_a = fct_drop(factor(factor_a, levels = factor.order)),
factor_b = fct_drop(factor(factor_b, levels = factor.order))
) %>%
arrange(factor_a, factor_b) %>%
group_by(factor_a) %>%
group_modify(~ add_row(.x)) %>%
mutate(
factor_a = as.character(factor_a),
factor_a = ifelse(is.na(factor_b), NA, factor_a)
) %>%
as.data.frame()
result_2_sheet = result.2 %>%
rownames_to_column("group") %>%
mutate(group = fct_drop(factor(group, levels = factor.order)) %>%
arrange(group) %>%
as.data.frame()
write.xlsx(result_1_sheet, file = "main.result.xlsx", col.names = TRUE, sheetName = "TukeyHSD_test")
write.xlsx(result_2_sheet, file = "main.result.xlsx", col.names = TRUE, row.names = FALSE,
sheetName = "descriptive_statistics", append = TRUE)
I have translated the code and left out any non-code content.
英文:
library(agricolae)
library(tidyverse)
library(xlsx)
main.data <- runif(256)
main.factor <- c("1st control", "2nd control", "Control A", "Control B",
"Variant A", "Variant B", "Variant C",
"1st Variant", "2nd Variant", "3rd Variant",
"Treatment 1", "Treatment 2", "Treatment 3",
"1st Treatment", "2nd Treatment", "3rd Treatment")
df <- data.frame(experiment=rep(main.factor,each=16), Main.value=main.data)
df$experiment <- factor(df$experiment, levels = c("1st control", "2nd control", "Control A", "Control B",
"Variant A", "Variant B", "Variant C",
"1st Variant", "2nd Variant", "3rd Variant",
"Treatment 1", "Treatment 2", "Treatment 3",
"1st Treatment", "2nd Treatment", "3rd Treatment"))
factor.order <- c("1st control", "2nd control", "Control A", "Control B", "Variant A", "Variant B", "Variant C", "1st Variant", "2nd Variant", "3rd Variant", "Treatment 1", "Treatment 2", "Treatment 3", "1st Treatment", "2nd Treatment", "3rd Treatment")
model.1 <- lm(Main.value~experiment, data=df)
result <- HSD.test(model.1, "experiment", group=F)
result.1 <- result$comparison
result.2 <- result$mean
result_1_sheet = result.1 %>%
rownames_to_column("grouping_factor") %>%
separate(grouping_factor, into = c("factor_a", "factor_b"), sep = " - ") %>%
mutate(
factor_a = fct_drop(factor(factor_a, levels = factor.order)),
factor_b = fct_drop(factor(factor_b, levels = factor.order))
) %>%
arrange(factor_a, factor_b) %>%
group_by(factor_a) %>%
group_modify(~ add_row(.x)) %>%
mutate(
factor_a = as.character(factor_a),
factor_a = ifelse(is.na(factor_b), NA, factor_a)
) %>%
as.data.frame()
result_2_sheet = result.2 %>%
rownames_to_column("group") %>%
mutate(group = fct_drop(factor(group, levels = factor.order))) %>%
arrange(group) %>%
as.data.frame()
write.xlsx(result_1_sheet, file = "main.result.xlsx", col.names = TRUE, sheetName = "TukeyHSD_test")
write.xlsx(result_2_sheet, file = "main.result.xlsx", col.names = TRUE, row.names = FALSE,
sheetName = "descriptive_statistics", append = TRUE)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论