这是我的载体
my_vec <- letters
是否可以使用 slice
(dplyr
) 或者 stringr
?
这是一个简单的排列。
最佳答案
你可以自己构建一个“插入”函数:
insert <- function(x, i, j){
tmp <- x[-i]
c(tmp[1:(j-1)], x[i], tmp[j:length(tmp)])
}
insert(letters, 26, 3)
# [1] "a" "b" "z" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t"
#[22] "u" "v" "w" "x" "y"
旧答案: 您可以自己构建一个置换函数:
permute <- function(x, i, j) {
x[c(i, j)] <- x[c(j, i)]
x}
permute(letters, 26, 3)
#[1] "a" "b" "z" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u"
#[22] "v" "w" "x" "y" "c"
https://stackoverflow.com/questions/72115277/