Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R语言中,创建一列用引号括起并用逗号分隔的标记名_R_String_Dplyr_Tidyverse_Mailchimp - Fatal编程技术网

在R语言中,创建一列用引号括起并用逗号分隔的标记名

在R语言中,创建一列用引号括起并用逗号分隔的标记名,r,string,dplyr,tidyverse,mailchimp,R,String,Dplyr,Tidyverse,Mailchimp,在R中,我试图为一个TIBLE创建一列,该列将包含一个标签字符串,用于在MailChimp中对电子邮件列表进行分段 我将上传到MailChimp的输出CSV中标记列的格式应该是字符串:“属性1”,“属性2” 我怀疑这样做将涉及使用if语句创建函数,并根据真/假逻辑添加标记 以下是数据结构的外观: library(tidyverse) person <- c("Person 1", "Person 2", "Person 3",

在R中,我试图为一个TIBLE创建一列,该列将包含一个标签字符串,用于在MailChimp中对电子邮件列表进行分段

我将上传到MailChimp的输出CSV中标记列的格式应该是字符串:“属性1”,“属性2”

我怀疑这样做将涉及使用if语句创建函数,并根据真/假逻辑添加标记

以下是数据结构的外观:

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"

库(tidyverse)

person我们可以将“name”的元素重新格式化为“long”格式,并在使用“value”列进行索引后粘贴到
中,并与原始数据集连接

library(dplyr)
library(tidyr)
library(stringr)
example_tbl %>%
    pivot_longer(cols = -person) %>%
    group_by(person) %>% 
    summarise(tag = str_c(dQuote(name[value], FALSE), collapse=", ")) %>% 
    left_join(example_tbl) %>%
    select(names(example_tbl), everything())
# A tibble: 4 x 5
#  person   is_in_A is_in_B is_in_C tag                                    
#  <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\""      
库(dplyr)
图书馆(tidyr)
图书馆(stringr)
示例\u tbl%>%
pivot_更长(cols=-人)%>%
分组单位(人)%>%
总结(tag=str_c(dQuote(name[value],FALSE),collapse=“,”))%>%
左连接(示例tbl)%>%
选择(名称(示例),所有内容()
#一个tibble:4x5
#人在A中是在B中是在C标签中
#                                                 
#1人1对真“\”在“A”中是“U”,在“B”中是“U”,在“C”中是“U”
#2人2假假真“\”在C中是“U”
#3人3对错对“\”在“A”中是“U”,在“C”中是“U”
#4人4假对真“\”在B中是“U”,在C中是“U”

基本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  

example\u tbl$Col使用
rowwise
c\u交叉

library(dplyr)
cols <- names(example_tbl)[-1]

example_tbl %>%
  rowwise() %>%
  mutate(tags = toString(dQuote(cols[c_across(starts_with('is_in'))])))

#  person   is_in_A is_in_B is_in_C tags                           
#  <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”           
example_tbl %>%
    mutate(tags = purrr::pmap_chr(select(., starts_with('is_in')), 
                  ~toString(cols[c(...)] )))