我试图让combn()在dplyr::mutate中工作,但我失败了,也不太明白为什么。
这是可行的:
c("a", "b", "c") %>% combn(2, FUN = paste, collapse = ";", simplify = TRUE)
[1] "a;b" "a;c" "b;c"但是我怎么才能让它工作呢?
tribble(
~col,
c("a", "b", "c"),
c("a", "d", "f")
) %>%
mutate(col = combn(str_split(names, ";"), 2, FUN = paste, collapse = ";")) 我希望矩阵中的每一行都是以下形式的字符向量:
[1] "a;b" "a;c" "b;c"上面的示例将是第一行。
编辑:我想如果不使用combn()也没问题。
发布于 2019-04-15 01:42:33
我们可以使用map在list和paste上循环
library(tidyverse)
out <- tribble(
~col,
c("a", "b", "c"),
c("a", "d", "f")
) %>%
mutate(col = map(col, ~ combn(.x, 2, FUN = paste, collapse=";"))) 发布于 2019-04-15 01:20:02
尝试:
tribble(
~col,
c("a", "b", "c"),
c("a", "d", "f")
) %>%
rowwise() %>%
mutate(new = toString(combn(col, 2, FUN = paste, collapse = ";")))https://stackoverflow.com/questions/55677783
复制相似问题