【发布时间】:2023-04-04 17:58:01
【问题描述】:
我正在尝试在一个 tibble 中创建一个新列,它是几个字符串列的串联。这些列的名称都符合一个模式......特别是,它们都以相同的子字符串开头。我正在尝试选择内部和外部mutate、paste、str_c 和unite 中的每一个组合,但无济于事。
代表:
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
我怎样才能做到这一点?
【问题讨论】:
-
这篇文章对你的问题有很多类似的建议 - stackoverflow.com/questions/52712390/…