【问题标题】:How to convert indicator columns to a concatenated column (of column names)如何将指示符列转换为连接列(列名)
【发布时间】:2019-02-28 02:55:15
【问题描述】:

我有 3 列由指标 (0/1) 组成

icols <- 
structure(list(delivery_group = c(0, 1, 1, 0, 0), culturally_tailored = c(0, 
0, 1, 0, 1), integrated_intervention = c(1, 0, 0, 0, 0)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -5L))

我想返回一个单一的字符列“限定符”,这样带有指示符 == 1 的列名连接在一个字符串中,如下所示:

*qualifiers*
    integrated_intervention
    delivery_group
    delivery_group, culturally_tailored

    culturally_tailored 

我尝试了 extdplyr::ind(使用各种选项)但没有成功。下面的一个使我的 R 会话崩溃。

icols <- extdplyr::ind_to_char(col = qualifiers, ret_factor = FALSE, remove = TRUE,
              from = c("delivery_group", "culturally_tailored", "integrated_intervention"),
              mutually_exclusive = FALSE, collectively_exhaustive = FALSE)

我找到了Convert Boolean indicator columns to a single factor column,但认为可能有更简单的解决方案。

【问题讨论】:

    标签: r dplyr tidyr


    【解决方案1】:

    你可以试试:

    icols$collapsed <- apply(icols, 1, function(x) paste0(names(icols)[x == 1], collapse = ", "))
    
    icols
    
      delivery_group culturally_tailored integrated_intervention                           collapsed
    1              0                   0                       1             integrated_intervention
    2              1                   0                       0                      delivery_group
    3              1                   1                       0 delivery_group, culturally_tailored
    4              0                   0                       0                                    
    5              0                   1                       0                 culturally_tailored
    

    或者,按照 Maurits 的建议,更简洁:

    apply(icols, 1, function(x) toString(names(icols)[x == 1]))
    

    【讨论】:

    • 不错的一个(+1)。为了让它更短,你可以使用toString 而不是paste0(..., collapse = ", ")apply(icols, 1, function(x) toString(names(icols)[x == 1]))
    【解决方案2】:

    我不确定这是一个“简单”的解决方案,但这里有一个使用 tidyverse 的解决方案。

    library(tidyverse)
    
    icols <- tibble(
      delivery_group = c(0, 1, 1, 0, 0),
      culturally_tailored = c(0, 0, 1, 0, 1),
      integrated_intervention = c(1, 0, 0, 0, 0)
    )
    
    icols %>%
      rowid_to_column(var = "rowid") %>%
      gather(key = "qualifiers", value = "indicator", -rowid) %>%
      filter(indicator == 1) %>%
      group_by(rowid) %>%
      summarize(qualifiers = paste(qualifiers, collapse = ", ")) %>%
      ungroup() %>%
      complete(rowid = 1:nrow(icols)) %>%
      select(qualifiers)
    #> # A tibble: 5 x 1
    #>   qualifiers                         
    #>   <chr>                              
    #> 1 integrated_intervention            
    #> 2 delivery_group                     
    #> 3 delivery_group, culturally_tailored
    #> 4 <NA>                               
    #> 5 culturally_tailored
    

    reprex package (v0.2.1) 于 2019 年 2 月 27 日创建

    【讨论】:

      【解决方案3】:

      这是一个疯狂的方式:

      library(tidyverse)
      
      icols %>%
        mutate(qualifiers = case_when(
          delivery_group & culturally_tailored == 1 ~ "delivery_group, culturally_tailored",
          delivery_group & integrated_intervention == 1 ~ "delivery_group, integrated_intervation",
          culturally_tailored & integrated_intervention == 1 ~ "culturally_tailored, integrated_intervation",
          culturally_tailored == 1 ~ "culturally_tailored",
          integrated_intervention == 1 ~ "integrated_intervention",
          delivery_group == 1 ~ "delivery_group"))
      # A tibble: 5 x 4
        delivery_group culturally_tailored integrated_intervention qualifiers                         
                 <dbl>               <dbl>                   <dbl> <chr>                              
      1              0                   0                       1 integrated_intervention            
      2              1                   0                       0 delivery_group                     
      3              1                   1                       0 delivery_group, culturally_tailored
      4              0                   0                       0 NA                                 
      5              0                   1                       0 culturally_tailored 
      
      
      

      【讨论】:

        猜你喜欢
        • 2015-12-14
        • 1970-01-01
        • 2015-07-31
        • 1970-01-01
        • 2019-06-06
        • 2021-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多