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 |
或者,按照 Maurits 的建议,更简洁:
1
|
apply(icols, 1, function(x) toString(names(icols)[x == 1]))
|
- 不错的一个(1)。为了让它更短,你可以使用 toString 而不是 paste0(…, collapse =”,”): 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( icols %>% |
由 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 %>% |
1
|
|
来源:https://www.codenong.com/54917703/