這是我對使用tidyverse
進行手動轉換的建議。 我已經把它作爲一個函數裝箱了,你可以通過傳遞除p_adj
以外的東西來更改度量。請注意,我假設輸入(tbl
)爲數據幀。
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 = as.numeric(substr(X, 1, 1)),
Var2 = as.numeric(substr(X, 3, 3))) %>%
# Only keep relevant fields
select(Var1, Var2, matches(metric)) %>%
# Filter out NA's
filter(!is.na(metric)) %>%
# Make into "wide" format using Var2
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')
輸出:
1 2 3 4 5
2 0.4873403
3 0.4219408 0.9999974
4 0.0206341 0.6147144 0.6866539
5 2e-06 0.0006595 0.0009888 0.084416
6 0.4219408 0.9999974 1 0.6866539 0.0009888
再生的數據集:
df <- structure(list(X = structure(c(1L, 2L, 4L, 7L, 11L, 3L, 5L, 8L,
12L, 6L, 9L, 13L, 10L, 14L, 15L), .Label = c("2-1", "3-1", "3-2",
"4-1", "4-2", "4-3", "5-1", "5-2", "5-3", "5-4", "6-1", "6-2",
"6-3", "6-4", "6-5"), class = "factor"), diff = c(0.213, 0.225,
0.388, 0.688, 0.225, 0.0125, 0.175, 0.475, 0.0125, 0.163, 0.463,
1.78e-15, 0.3, -0.163, -0.463), lwr = c(-0.13653578, -0.12403578,
0.03846422, 0.33846422, -0.12403578, -0.31064434, -0.14814434,
0.15185566, -0.31064434, -0.16064434, 0.13935566, -0.32314434,
-0.02314434, -0.48564434, -0.78564434), upr = c(0.5615358, 0.5740358,
0.7365358, 1.0365358, 0.5740358, 0.3356443, 0.4981443, 0.7981443,
0.3356443, 0.4856443, 0.7856443, 0.3231443, 0.6231443, 0.1606443,
-0.1393557), p_adj = c(0.4873403, 0.4219408, 0.0206341, 2e-06,
0.4219408, 0.9999974, 0.6147144, 0.0006595, 0.9999974, 0.6866539,
0.0009888, 1, 0.084416, 0.6866539, 0.0009888)), .Names = c("X",
"diff", "lwr", "upr", "p_adj"), class = "data.frame", row.names = c(NA,
-15L))