### 1. doCombn函数可以给出因子水平的两两比较的组合
doCombn <- function(x){
xx <- unique(x)
combns <- combn(1:length(xx), 2)
res <- apply(combns, 2, function(x) str_glue("{xx[x[1]]} vs {xx[x[2]]}") )
return(res)
}
doCombn(c("A", "B", "C"))
doCombn2 <- function(x){
xx <- unique(x)
combns <- as.data.frame(combn(xx, 2))
res <- map_chr(combns,~paste0(.x,collapse = " vs "))
res
}
doCombn2(c("A", "B", "C"))
如何讲多层列表转变成单层列表?
list.flatten(list1)
test1 <-list(
KEGG = list(
'AvsB' = c(1, 2),
'BvsC' = '2'
),
GO = list(
'AvsB' = 3,
'BvsC' = 4
)
)
test1
# $KEGG
#$KEGG$AvsB
#[1] 1 2
#
#$KEGG$BvsC
#[1] "2"
#
#
#$GO
#$GO$AvsB
#[1] 3
#
#$GO$BvsC
#[1] 4
test1 %>%
enframe(name = "name2", value = "value2") %>%
unnest(value2) %>%
mutate(value2 = enframe(value2)) %>%
unnest(value2) %>%
mutate(group = str_c(name2, name, sep = "---")) %>%
dplyr::select(group, value) %>%
deframe()