0

我有 2 个向量xw. 我想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
4

1 回答 1

3

为什么不这样做:

 x[w[1:z]]
[1] 1.3 1.5 1.6 5.3 7.8
于 2013-06-14T01:32:42.850 回答