问题
What the input would be:
c("a", "b", "c")
[1] "a" "b" "c"
I want a function that returns:
[1] "a;b" "a;c" "b;c"
I need this function to work solely off its inputs. I've tried some stuff with purrr::map()
andpurrr::reduce()
, but I haven't managed to get anything useful.
回答1:
We can use combn
from base R with FUN
argument as paste
combn(x, 2, FUN = paste, collapse = ";")
#[1] "a;b" "a;c" "b;c"
data
x <- c("a", "b", "c")
回答2:
Not the exact result but maybe it might be useful:
test<-c("a", "b", "c")
lapply(test,function(x) paste0(x,";",setdiff(test,x)))
Result:
[[1]]
[1] "a;b" "a;c"
[[2]]
[1] "b;a" "b;c"
[[3]]
[1] "c;a" "c;b"
来源:https://stackoverflow.com/questions/55674350/how-do-i-write-a-function-to-convert-a-character-vector-into-a-character-vector