tidyverse 跨列连接字符不适用于 c_across
tidyverse concatenate characters across columns does not work with c_across
我有以下数据:
dat <- data.frame(sheet = c("a", "b", "c"),
condition1 = c("k", "l", "m"),
condition2 = c("x", "y", "z"))
我现在想使用连接这些列的 tidyverse 创建一个新列。但是,我总是出错。
library(tidyverse)
new_dat %>%
rowwise() %>%
mutate(marker = str_c(c_across(c(sheet, starts_with("condition"))), sep = "/")) %>%
ungroup()
Error: Problem with `mutate()` input `marker`.
x Input `marker` can't be recycled to size 1.
i Input `marker` is `str_c(c_across(c(sheet, starts_with("condition"))), sep = "/")`.
i Input `marker` must be size 1, not 3.
i Did you mean: `marker = list(str_c(c_across(c(sheet, starts_with("condition"))), sep = "/"))` ?
i The error occurred in row 1.
任何suggestion/help?
预期输出:
sheet condition1 condition2 marker
1 a k x a/k/x
2 b l y b/l/y
3 c m z c/m/z
注意:我已经在不使用 rowwise 部分并使用 across
而不是 c_across
的情况下尝试了代码(并调整了横跨的语法),但我遇到了类似的错误。将 str_c
替换为 paste0
也不起作用。
编辑:从输入数据中删除了 x 列,因为它没有添加任何值。
使用collapse
,而不是sep
:
dat %>%
rowwise() %>%
mutate(marker = str_c(c_across(c(sheet, starts_with("condition"))),
collapse = "/")) %>%
ungroup()
# A tibble: 3 x 5
sheet condition1 condition2 x marker
<fct> <fct> <fct> <dbl> <chr>
1 a k x 1 a/k/x
2 b l y 2 b/l/y
3 c m z 3 c/m/z
您可以通过这个简单的示例了解 sep
和 collapse
之间的区别(展示比解释更容易):
str_c(letters[1:3], letters[4:6], sep = "_")
# "a_d" "b_e" "c_f"
str_c(letters[1:3], letters[4:6], collapse = "_")
# "ad_be_cf"
您可以使用 tidyr
的 unite
:
tidyr::unite(dat, marker, sheet, starts_with('condition'),
sep = '/', remove = FALSE)
# marker sheet condition1 condition2 x
#1 a/k/x a k x 1
#2 b/l/y b l y 2
#3 c/m/z c m z 3
结合使用 dplyr
和 purrr
,您可以:
dat %>%
mutate(marker = pmap(across(c(sheet, starts_with("condition"))), paste, sep = "/"))
sheet condition1 condition2 x marker
1 a k x 1 a/k/x
2 b l y 2 b/l/y
3 c m z 3 c/m/z
我有以下数据:
dat <- data.frame(sheet = c("a", "b", "c"),
condition1 = c("k", "l", "m"),
condition2 = c("x", "y", "z"))
我现在想使用连接这些列的 tidyverse 创建一个新列。但是,我总是出错。
library(tidyverse)
new_dat %>%
rowwise() %>%
mutate(marker = str_c(c_across(c(sheet, starts_with("condition"))), sep = "/")) %>%
ungroup()
Error: Problem with `mutate()` input `marker`.
x Input `marker` can't be recycled to size 1.
i Input `marker` is `str_c(c_across(c(sheet, starts_with("condition"))), sep = "/")`.
i Input `marker` must be size 1, not 3.
i Did you mean: `marker = list(str_c(c_across(c(sheet, starts_with("condition"))), sep = "/"))` ?
i The error occurred in row 1.
任何suggestion/help?
预期输出:
sheet condition1 condition2 marker
1 a k x a/k/x
2 b l y b/l/y
3 c m z c/m/z
注意:我已经在不使用 rowwise 部分并使用 across
而不是 c_across
的情况下尝试了代码(并调整了横跨的语法),但我遇到了类似的错误。将 str_c
替换为 paste0
也不起作用。
编辑:从输入数据中删除了 x 列,因为它没有添加任何值。
使用collapse
,而不是sep
:
dat %>%
rowwise() %>%
mutate(marker = str_c(c_across(c(sheet, starts_with("condition"))),
collapse = "/")) %>%
ungroup()
# A tibble: 3 x 5
sheet condition1 condition2 x marker
<fct> <fct> <fct> <dbl> <chr>
1 a k x 1 a/k/x
2 b l y 2 b/l/y
3 c m z 3 c/m/z
您可以通过这个简单的示例了解 sep
和 collapse
之间的区别(展示比解释更容易):
str_c(letters[1:3], letters[4:6], sep = "_")
# "a_d" "b_e" "c_f"
str_c(letters[1:3], letters[4:6], collapse = "_")
# "ad_be_cf"
您可以使用 tidyr
的 unite
:
tidyr::unite(dat, marker, sheet, starts_with('condition'),
sep = '/', remove = FALSE)
# marker sheet condition1 condition2 x
#1 a/k/x a k x 1
#2 b/l/y b l y 2
#3 c/m/z c m z 3
结合使用 dplyr
和 purrr
,您可以:
dat %>%
mutate(marker = pmap(across(c(sheet, starts_with("condition"))), paste, sep = "/"))
sheet condition1 condition2 x marker
1 a k x 1 a/k/x
2 b l y 2 b/l/y
3 c m z 3 c/m/z