我有 2 个向量x
和w
. 我想x
根据 order ofw
和 lengths提取 vector 的元素z
。我怎样才能在 R 中更轻松、更快地做到这一点?
x = c(1.3, 1.5, 1.6, 2.4, 5.3, 6.6, 7.8)
w = c(1, 2, 3, 5, 7, 4, 6)
z = 5
m = matrix(c(x[w], w), ncol=2)
> m
[,1] [,2]
[1,] 1.3 1
[2,] 1.5 2
[3,] 1.6 3
[4,] 5.3 5
[5,] 7.8 7
[6,] 2.4 4
[7,] 6.6 6
f = m[1:z,1]
> f
[1] 1.3 1.5 1.6 5.3 7.8