R 如何将指示符列转换为(列名的)连接列

R 如何将指示符列转换为(列名的)连接列,r,dplyr,tidyr,R,Dplyr,Tidyr,我有3列,由指标(0/1)组成 我尝试了extdplyr::ind(使用各种选项),但没有成功。下面的那个使我的R会话崩溃了 icols <- extdplyr::ind_to_char(col = qualifiers, ret_factor = FALSE, remove = TRUE, from = c("delivery_group", "culturally_tailored", "integrated_intervention"),

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

我尝试了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)
icols我不确定这是一个“简单”的解决方案,但这里有一个使用tidyverse的解决方案

库(tidyverse)
icols%
rowid_to_列(var=“rowid”)%%>%
聚集(key=“qualifiers”、value=“indicator”、-rowid)%>%
过滤器(指示器==1)%>%
分组依据(rowid)%>%
汇总(限定符=粘贴(限定符,折叠“”,“”)%>%
解组()%>%
完成(rowid=1:nrow(icols))%>%
选择(限定符)
#>#tible:5 x 1
#>限定词
#>                                 
#>1综合干预
#>2交付组
#>3交付组,文化定制
#> 4                                
#>5文化定制
由(v0.2.1)创建于2019-02-27这里有一个疯狂的方式:

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 
您可以尝试:

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
不错的一个(+1)。要使其更短,您可以使用
toString
而不是
paste0(…,collapse=“,”)
apply(icols,1,函数(x)toString(名称(icols)[x==1])
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 

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
apply(icols, 1, function(x) toString(names(icols)[x == 1]))