谁能告诉我如何在有序向量中插入数字。
假设我的向量是x <- c(4, 6, 9, 10, 13, 15, 19)?我想11在10和之间插入13?您能告诉我如何找出插入的列并找出其在新向量中的位置吗?
非常感谢!
使用order,也许您可以做到:
x <- c(4, 6, 9, 10, 13, 15, 19)
ins <- 11
point <- which(order(c(ins,x))==1)
point
#[1] 5
append(x, ins, point-1)
#[1] 4 6 9 10 11 13 15 19
Run Code Online (Sandbox Code Playgroud)
可能更直接的替代方法是?Position:
point <- Position(function(v) v < ins, x, right=TRUE)
append(x, ins, after=point)
#[1] 4 6 9 10 11 13 15 19
Run Code Online (Sandbox Code Playgroud)