如何更改向量的某些元素

时间:2014-05-12 18:04:33

标签: r

我有一个数字x向量和另一个数字向量y

x <- c(0.1,0.3,0.2,0.3,0.12,0.1,0.24,0.05,
     0.03,0.22,0.04,0.003,0.21,0.11,0.44,0.52)

y <- c(0.25,0.054,0.09,.0.93,0.23)

我在x中的元素的订单位置,我希望通过y更改它们。

3,5,6,9,14

我想仅使用x中元素的位置来做到这一点我想改变y的那些因此我将获得

x <- c(0.1,0.3,0.25,0.3,0.054,0.09,0.24,0.05,
       0.93,0.22,0.04,0.003,0.21,0.23,0.44,0.52)

符合

x <- replace(x, (3,5,6,9,14), (0.25,0.054,0.09,.0.93,0.23))
非常感谢

1 个答案:

答案 0 :(得分:2)

> x <- c(0.1, 0.3, 0.2, 0.3, 0.12, 0.1, 0.24, 0.05,
         0.03, 0.22, 0.04, 0.003, 0.21, 0.11, 0.44, 0.52)
> y <- c(0.25, 0.054, 0.09, 0.93, 0.23)

按指定的索引更改x值只是

> x[c(3, 5, 6, 9, 14)] <- y
> x
## [1] 0.100 0.300 0.250 0.300 0.054 0.090 0.240 0.050 
## [9] 0.930 0.220 0.040 0.003 0.210 0.230 0.440 0.520