1. ### 1. doCombn函数可以给出因子水平的两两比较的组合
  2. doCombn <- function(x){
  3. xx <- unique(x)
  4. combns <- combn(1:length(xx), 2)
  5. res <- apply(combns, 2, function(x) str_glue("{xx[x[1]]} vs {xx[x[2]]}") )
  6. return(res)
  7. }
  8. doCombn(c("A", "B", "C"))
  9. doCombn2 <- function(x){
  10. xx <- unique(x)
  11. combns <- as.data.frame(combn(xx, 2))
  12. res <- map_chr(combns,~paste0(.x,collapse = " vs "))
  13. res
  14. }
  15. doCombn2(c("A", "B", "C"))

如何讲多层列表转变成单层列表?

  1. list.flatten(list1)
  2. test1 <-list(
  3. KEGG = list(
  4. 'AvsB' = c(1, 2),
  5. 'BvsC' = '2'
  6. ),
  7. GO = list(
  8. 'AvsB' = 3,
  9. 'BvsC' = 4
  10. )
  11. )
  12. test1
  13. # $KEGG
  14. #$KEGG$AvsB
  15. #[1] 1 2
  16. #
  17. #$KEGG$BvsC
  18. #[1] "2"
  19. #
  20. #
  21. #$GO
  22. #$GO$AvsB
  23. #[1] 3
  24. #
  25. #$GO$BvsC
  26. #[1] 4
  27. test1 %>%
  28. enframe(name = "name2", value = "value2") %>%
  29. unnest(value2) %>%
  30. mutate(value2 = enframe(value2)) %>%
  31. unnest(value2) %>%
  32. mutate(group = str_c(name2, name, sep = "---")) %>%
  33. dplyr::select(group, value) %>%
  34. deframe()