Suppose I have three vectors a.1, a.2, a.3 -- they could hold arbitrary values (meaning the values would not necessarily be sorted)
> a.1 <- seq(1,15,3)
> a.2 <- seq(15,2,-3)
> a.3 <- seq(2,15,3)
> a.1
[1] 1 4 7 10 13
> a.2
[1] 15 12 9 6 3
> a.3
[1] 2 5 8 11 14
Now I want to produce a new vector C where I take all elements of a except elements at indices 2, 3, 5 and only elements at indices 2, 3 from vector a.2 and element at index 5 from a.3; combine them in order
One way I know how to do this is:
> C <- a.1
> C[c(2,3)] <- a.2[c(2,3)]
> C[5] <- a.3[5]
> C
[1] 1 12 9 10 14
I wonder if there are are better ways of doing this -- better may include:
- More elegant
- Extendable to say splices from multiple elements
- More Terse (syntax wise)
Notes
- One of the answer authors has suggested using replace -- I wonder if replace can be used in multiway splices
- Also note the vales in the source or target vector need not be sorted
- It would be great if the same solution can be used (barring some minor modifications) on both vectors and dataframes