连接 R 中的列,但仅在值存在时保留分隔符

Concatenate columns in R, but only keep separator if value exists

有谁知道 R 中连接 n 列但仅在该行中有值时才保留分隔符的方法?如果您 运行 下面的示例:

df <- data.frame(
                  name1 = c("Jim","Bob","Sue"),
                  name2 = c("Jane","","Bane"),
                  name3 = c('Conor',"",""),
                  name4 = c("","","Bonor")
                )

df$names <- paste(df$name1,df$name2,df$name3, sep=";")

即使单元格为空,您也会看到末尾和中间值包含分隔符,输出为:

df =

name1 name2 name3 name4  names
Jim   Jane  Conor        Jim;Jane;Conor;
Bob                      Bob;;;
Sue   Bane        Bonor  Sue;Bane;;Bonor

在单元格为空的情况下,是否有任何方法可以不包含或删除分隔符?有了预期的结果:

df =

name1 name2 name3 name4  names
Jim   Jane  Conor        Jim;Jane;Conor
Bob                      Bob
Sue   Bane        Bonor  Sue;Bane;Bonor
library(dplyr)
library(tidyr)

df %>% 
  mutate_all(na_if,"") %>% 
  unite("names", everything(), sep = ";", remove = F, na.rm = T)

#>            names name1 name2 name3 name4
#> 1 Jim;Jane;Conor   Jim  Jane Conor  <NA>
#> 2            Bob   Bob  <NA>  <NA>  <NA>
#> 3 Sue;Bane;Bonor   Sue  Bane  <NA> Bonor

更新:将此解决方案应用于特定列。

我正在根据下面的评论修改 akrun 的回答;

df %>% 
  mutate(across(c("name1", "name2", "name3", "name4"), na_if, "", 
                .names = "{.col}_changed")) %>% 
  unite(names, ends_with('_changed'), na.rm = TRUE, sep = ";")

#>   name1 name2 name3 name4          names
#> 1   Jim  Jane Conor       Jim;Jane;Conor
#> 2   Bob                              Bob
#> 3   Sue  Bane       Bonor Sue;Bane;Bonor