将来自符合模式的名称的字符串粘贴在一起并忽略 NA

Paste together strings which come from names that fit a pattern and ignore NAs

我正在尝试在 tibble 中创建一个新列,它是几个字符串列的串联。这些列的名称都符合某种模式……特别是,它们都以相同的子字符串开头。我正在尝试选择内部和外部 mutate、我能想到的每个 pastestr_cunite 的所有组合,但无济于事。

代表:

library(tibble); library(dplyr)
df <- tibble(
    include1 = c("a", "b", "c"),
    include2 = c("d", "e", NA),
    include3 = c("f", "g", "h"),
    include4 = c("i", NA, NA),
    ignore = c("j", "k", "l")
    )

df
# A tibble: 3 x 5
  include1 include2 include3 include4 ignore
  <chr>    <chr>    <chr>    <chr>    <chr> 
1 a        d        f        i        j     
2 b        e        g        NA       k     
3 c        NA       h        NA       l     

我正在尝试看起来像变体的代码:

df %>% 
    mutate(included = str_c(starts_with("include"), " | ", na.rm = TRUE)) %>% 
    select(ignore, included)

预期输出:

# A tibble: 3 x 2
  ignore included     
  <chr>  <chr>        
1 j      a | d | f | i
2 k      b | e | g    
3 l      c | h    

我该如何实现?

你可以这样做:

library(dplyr)
library(purrr)

df %>%
  transmute(ignore, 
            included = pmap_chr(df %>% select(-ignore), ~ paste(na.omit(c(...)), collapse = " | ")))

# A tibble: 3 x 2
  ignore included     
  <chr>  <chr>        
1 j      a | d | f | i
2 k      b | e | g    
3 l      c | h        

我们可以使用 unitena.rm

library(dplyr)
library(tidyr)
df %>%
    unite(included, starts_with('include'), na.rm = TRUE, sep = "| ") %>%
   select(ignore, included)

-输出

# A tibble: 3 x 2
#  ignore included  
#  <chr>  <chr>     
#1 j      a| d| f| i
#2 k      b| e| g   
#3 l      c| h