在 R 中扩展电子邮件数据集的标题

Expand tibble of email dataset in R

我有大量电子邮件数据,如下所示:

library(dplyr)

emails <- tibble(
  from = c('employee.1@xtra.co','employee.5@xtra.co','employee.1@xtra.co',
           'employee.3@xtra.co','employee.1@xtra.co'),
  to = list(
    c('employee.5@xtra.co', 'employee.3xtra.co'),
    c('employee.3@xtra.co', 'employee.1@xtra.co'),
    c('employee.2@xtra.co'),
    c('employee.1@xtra.co'),
    c('employee.3@xtra.co','employee.5@xtra.co','employee.6@xtra.co')),
  
  cc = list(
    c('employee.2xtra.co', 'employee.4xtra.co', 'employee.6xtra.co'),
    c('employee.1xtra.co', 'employee.8xtra.co', 'employee.6xtra.co'),
    NA,
    c('employee.2xtra.co', 'employee.4xtra.co'),
    c('employee.2xtra.co', 'employee.6xtra.co'))
)

emails

# A tibble: 5 x 3
  from               to        cc       
  <chr>              <list>    <list>   
1 employee.1@xtra.co <chr [2]> <chr [3]>
2 employee.5@xtra.co <chr [2]> <chr [3]>
3 employee.1@xtra.co <chr [1]> <lgl [1]>
4 employee.3@xtra.co <chr [1]> <chr [2]>
5 employee.1@xtra.co <chr [3]> <chr [2]>

我需要你的帮助才能为每个组合扩展每条记录。例如,我想为第 1 行实现的是:

from                to                  cc
employee.1@xtra.co  employee.5@xtra.co  employee.2xtra.co
employee.1@xtra.co  employee.5@xtra.co  employee.4xtra.co
employee.1@xtra.co  employee.5@xtra.co  employee.6xtra.co
employee.1@xtra.co  employee.3xtra.co   employee.2xtra.co
employee.1@xtra.co  employee.3xtra.co   employee.4xtra.co
employee.1@xtra.co  employee.3xtra.co   employee.6xtra.co

非常感谢您的宝贵时间。

我们可以申请unnest两次。

library(dplyr)
library(tidyr)

emails2 <- emails %>%
  unnest(cols = "to") %>%
  unnest(cols = "cc")
head(emails2)
# # A tibble: 6 x 3
#   from               to                 cc               
#   <chr>              <chr>              <chr>            
# 1 employee.1@xtra.co employee.5@xtra.co employee.2xtra.co
# 2 employee.1@xtra.co employee.5@xtra.co employee.4xtra.co
# 3 employee.1@xtra.co employee.5@xtra.co employee.6xtra.co
# 4 employee.1@xtra.co employee.3xtra.co  employee.2xtra.co
# 5 employee.1@xtra.co employee.3xtra.co  employee.4xtra.co
# 6 employee.1@xtra.co employee.3xtra.co  employee.6xtra.co

如果要展开的列超过两列,下面是一种方法。首先确定列出的列。将列名存储在 names_target 中,然后使用 for 循环重复应用 unnest 函数。

names_target <- emails %>%
  select(where(is.list)) %>%
  names()

temp <- emails

for (i in names_target){
  temp <- temp %>% unnest(cols = all_of(i))
}

identical(temp, emails2)
# [1] TRUE