我的数据由一个标识符(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]>


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

r - 将列表列转换为字符串列-LMLPHP

最佳答案

您还可以使用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

08-19 13:12