In R, I am trying to create a column for a tibble that will contain a string of tags that will be used to segment an email list in MailChimp.
The format of the tags column in the output CSV that I will upload to MailChimp should be a string: "Attribute 1", "Attribute 2"
I suspect that doing this will involve creating a function with an if statement, and adding a tag based upon a TRUE/FALSE logical.
Here is what the data structure would look like:
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
This is what the output should look like:
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"
Any suggestions would be welcome.