关于 r:如何将指标列转换为连接列(列名)

How to convert indicator columns to a concatenated column (of column names)

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

组成

1
2
3
4
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))

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

1
2
3
4
5
6
*qualifiers*
    integrated_intervention
    delivery_group
    delivery_group, culturally_tailored

    culturally_tailored

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

1
2
3
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)

我发现将布尔指标列转换为单因子列,但认为可能有更简单的解决方案。


你可以试试:

1
2
3
4
5
6
7
8
9
10
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 的建议,更简洁:

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


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 包 (v0.2.1) 创建于 2019-02-27


这是一个疯狂的方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
1