R 如何将TukeyHSD的结果排列到表格中?
我在下面有一个TukeyHSD的输出(转换为数据帧): 我已经尝试过类似的解决方案 我修改了代码以识别连字符“-”上的拆分,但它不起作用(见下文)。如果有人能找出它为什么不能执行或使用其他方法,我将非常感激R 如何将TukeyHSD的结果排列到表格中?,r,R,我在下面有一个TukeyHSD的输出(转换为数据帧): 我已经尝试过类似的解决方案 我修改了代码以识别连字符“-”上的拆分,但它不起作用(见下文)。如果有人能找出它为什么不能执行或使用其他方法,我将非常感激 transformTable <- function(tbl, metric) { # Takes table of TurkeyHSD output metrics # and transforms them into a pairwise comparison matrix
transformTable <- function(tbl, metric) {
# Takes table of TurkeyHSD output metrics
# and transforms them into a pairwise comparison matrix.
# tbl is assumed to be a data.frame or tibble,
# var is assumed to be a character string
# giving the variable name of the metric in question
# (here: "diff", "lwr", "upr", or "p_adj")
tbl <- tbl %>%
# Split comparison into individual variables
mutate(
Var1 = sub("\\-.*", "", comp), #before hypen
Var2 = sub(".*-", "", comp)) # after hyphen%>%
# Only keep relevant fields
select(Var1, Var2, matches(metric)) %>%
# Filter out NA's
filter(!is.na(metric)) %>%
# Make into "wide" format using Va r2
spread_(key = 'Var2', value = metric, fill = '')
# Let's change the row names to Var1
row.names(tbl) <- tbl$Var1
# And drop the Var1 column
tbl <- select(tbl, -Var1)
return(tbl)
}
又快又脏的方法
library(tidyr)
library(stringr)
df <- df %>%
separate(col = comp, into = c('x', 'y'), sep = '-') %>%
mutate(x = str_remove(x, ":")) %>%
mutate(y = str_remove(y, ":")) %>%
select(x, y, p_adj)
df1 <- data.frame(matrix(nrow = length(unique(c(df$x, df$y))), ncol = length(unique(c(df$x, df$y)))))
colnames(df1) <- unique(c(df$x, df$y))
rownames(df1) <- unique(c(df$x, df$y))
for(i in 1:length(unique(c(df$x, df$y)))){
for(j in 1:length(unique(c(df$x, df$y)))){
value <- (df %>% filter(x == rownames(df1)[i]) %>% filter(y == colnames(df1)[j]) %>% select(p_adj))$p_adj
if(length(value) != 0){
df1[i,j] <-value
df1[j,i] <- value
}
}
}
library(tidyr)
图书馆(stringr)
df%
分离(col=comp,into=c('x','y'),sep='-'))%>%
变异(x=str_remove(x,“:”)%%>%
突变(y=str_删除(y,“:”)%%
选择(x,y,p_调整)
df1
transformTable <- function(tbl, metric) {
# Takes table of TurkeyHSD output metrics
# and transforms them into a pairwise comparison matrix.
# tbl is assumed to be a data.frame or tibble,
# var is assumed to be a character string
# giving the variable name of the metric in question
# (here: "diff", "lwr", "upr", or "p_adj")
tbl <- tbl %>%
# Split comparison into individual variables
mutate(
Var1 = sub("\\-.*", "", comp), #before hypen
Var2 = sub(".*-", "", comp)) # after hyphen%>%
# Only keep relevant fields
select(Var1, Var2, matches(metric)) %>%
# Filter out NA's
filter(!is.na(metric)) %>%
# Make into "wide" format using Va r2
spread_(key = 'Var2', value = metric, fill = '')
# Let's change the row names to Var1
row.names(tbl) <- tbl$Var1
# And drop the Var1 column
tbl <- select(tbl, -Var1)
return(tbl)
}
transformTable(df,'p_adj')
library(tidyr)
library(stringr)
df <- df %>%
separate(col = comp, into = c('x', 'y'), sep = '-') %>%
mutate(x = str_remove(x, ":")) %>%
mutate(y = str_remove(y, ":")) %>%
select(x, y, p_adj)
df1 <- data.frame(matrix(nrow = length(unique(c(df$x, df$y))), ncol = length(unique(c(df$x, df$y)))))
colnames(df1) <- unique(c(df$x, df$y))
rownames(df1) <- unique(c(df$x, df$y))
for(i in 1:length(unique(c(df$x, df$y)))){
for(j in 1:length(unique(c(df$x, df$y)))){
value <- (df %>% filter(x == rownames(df1)[i]) %>% filter(y == colnames(df1)[j]) %>% select(p_adj))$p_adj
if(length(value) != 0){
df1[i,j] <-value
df1[j,i] <- value
}
}
}