将组合变量 (combn) 取消嵌套为向量

Unnesting a combination variable (combn) as a vector

使用下面的代码,我设法得到了罚款 combination :

tibble(
    x = list(c(1, 2, 3), c(4,5,6))
  ) %>% 
  mutate(
      combination = 
        x %>% 
        map(
            .f = combn
          , 2
          ) %>% 
        map(.f = t)
    ) %>% 
  unnest(combination)

# A tibble: 6 x 2
  x         combination[,1]  [,2]
  <list>              <dbl> <dbl>
1 <dbl [3]>               1     2
2 <dbl [3]>               1     3
3 <dbl [3]>               2     3
4 <dbl [3]>               4     5
5 <dbl [3]>               4     6
6 <dbl [3]>               5     6

然而,当使用 View() 函数观察时,我得到:

如何才能将 combination 显示为矢量?即:

我们可以将combn中的simplify = FALSE指定为return一个list而不是强制为matrix

library(purrr)
library(dplyr)
library(tidyr)
tbl1 <- tibble(
   x = list(c(1, 2, 3), c(4,5,6))
  ) %>% 
 mutate(
  combination = 
    x %>% 
    map(
        .f = combn
      , 2, simplify = FALSE
      )) 

现在,执行 unnest

out <- tbl1 %>%
   unnest(combination)

out
# A tibble: 6 x 2
#  x         combination
#  <list>    <list>     
#1 <dbl [3]> <dbl [2]>  
#2 <dbl [3]> <dbl [2]>  
#3 <dbl [3]> <dbl [2]>  
#4 <dbl [3]> <dbl [2]>  
#5 <dbl [3]> <dbl [2]>  
#6 <dbl [3]> <dbl [2]>  

检查 View

这是一个可能有用的data.table选项

library(data.table)
library(tidyr)
unnest(setDT(df)[, combination := lapply(x, function(v) combn(v, 2, simplify = FALSE))], combination)