在 R 中,我尝试为 tibble 创建一个列,其中包含一串标签,这些标签将用于在 MailChimp 中对电子邮件列表进行分段。
我将上传到 MailChimp 的输出 CSV 中标签列的格式应该是字符串:“属性 1”、“属性 2”
我怀疑这样做将涉及使用 if 语句创建一个函数,并添加基于 TRUE/FALSE 逻辑的标签。
数据结构如下:
library(tidyverse)
person <- c("Person 1", "Person 2", "Person 3", "Person 4")
is_in_A <- c(TRUE, FALSE, TRUE, FALSE)
is_in_B <- c(TRUE, FALSE, FALSE, TRUE)
is_in_C <- c(TRUE, TRUE, TRUE, TRUE)
example_tbl <- tibble(person = person, is_in_A = is_in_A, is_in_B, is_in_C = is_in_C)
this is what the data looks like:
example_tbl
# person is_in_A is_in_B is_in_C
# <chr> <lgl> <lgl> <lgl>
# Person 1 TRUE TRUE TRUE
# Person 2 FALSE FALSE TRUE
# Person 3 TRUE FALSE TRUE
# Person 4 FALSE TRUE TRUE
输出应如下所示:
tags_1 <- '"is in A", "is in B", "is in C"'
tags_2 <- '"is in C"'
tags_3 <- '"is in A", "is in C"'
tags_4 <- '"is in B", "is in C"'
all_tags <- c(tags_1, tags_2, tags_3, tags_4)
example_output_tbl <- example_tbl %>%
add_column(tags = all_tags)
# this is what the output should look like
example_output_tbl
# person is_in_A is_in_B is_in_C tags
# <chr> <lgl> <lgl> <lgl> <chr>
# Person 1 TRUE TRUE TRUE "is in A", "is in B", "is in C"
# Person 2 FALSE FALSE TRUE "is in C"
# Person 3 TRUE FALSE TRUE "is in A", "is in C"
# Person 4 FALSE TRUE TRUE "is in B", "is in C"
欢迎任何建议。
最佳答案
这个基础 R
可以帮助:
example_tbl$Col <- apply(example_tbl[,-1],1,function(x) paste(names(example_tbl[,-1])[which(x==T)],collapse = ','))
# A tibble: 4 x 5
person is_in_A is_in_B is_in_C Col
<chr> <lgl> <lgl> <lgl> <chr>
1 Person 1 TRUE TRUE TRUE is_in_A,is_in_B,is_in_C
2 Person 2 FALSE FALSE TRUE is_in_C
3 Person 3 TRUE FALSE TRUE is_in_A,is_in_C
4 Person 4 FALSE TRUE TRUE is_in_B,is_in_C
关于r - 在 R 中,创建一列用引号括起来并用逗号分隔的标签名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63106365/