将列表列转换为字符串列

时间:2019-05-13 23:00:26

标签: r dplyr tostring purrr

我的数据由一个标识符(srdr_id)和一个列表列组成。

dat <- structure(list(srdr_id = c("174136", "174258", "174684"), outcomes = list(
    structure(list(outcome_s = c("use_alcohol", "use_cannabis", 
    "use_cocaine")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
    -3L)), structure(list(outcome_s = "use_methamphetamine"), class = c("tbl_df", 
    "tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(
        outcome_s = c("use_alcohol", "use_heavy")), class = c("tbl_df", 
    "tbl", "data.frame"), row.names = c(NA, -2L)))), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -3L))

> dat
# A tibble: 3 x 2
  srdr_id outcomes        
  <chr>   <list>          
1 174136  <tibble [3 x 1]>
2 174258  <tibble [1 x 1]>
3 174684  <tibble [2 x 1]>

我想将结果中的每个小标题转换为一个逗号分隔的字符串。

enter image description here

3 个答案:

答案 0 :(得分:2)

这里是tidyverse的一种方式-

dat %>% 
  unnest(outcomes) %>% 
  group_by(srdr_id) %>% 
  summarise(
    outcomes = toString(outcome_s)
  )

# A tibble: 3 x 2
  srdr_id outcomes                              
  <chr>   <chr>                                 
1 174136  use_alcohol, use_cannabis, use_cocaine
2 174258  use_methamphetamine                   
3 174684  use_alcohol, use_heavy        

答案 1 :(得分:2)

您还可以使用map函数遍历列表列,拉出每个小标题的第一列并折叠为单个字符串:

library(tidyverse)
dat <- structure(list(srdr_id = c("174136", "174258", "174684"), outcomes = list(structure(list(outcome_s = c("use_alcohol", "use_cannabis", "use_cocaine")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -3L)), structure(list(outcome_s = "use_methamphetamine"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -1L)), structure(list(outcome_s = c("use_alcohol", "use_heavy")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -2L)))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -3L))
dat %>%
  mutate(outcomes = map_chr(outcomes, ~ .[[1]] %>% str_c(collapse = ", ")))
#> # A tibble: 3 x 2
#>   srdr_id outcomes                              
#>   <chr>   <chr>                                 
#> 1 174136  use_alcohol, use_cannabis, use_cocaine
#> 2 174258  use_methamphetamine                   
#> 3 174684  use_alcohol, use_heavy

reprex package(v0.2.1)于2019-05-13创建

答案 2 :(得分:0)

您现在可以使用

dat %>% rowwise() %>% 
    mutate(outcomes = paste(outcomes, collapse=',')) %>%
    ungroup()