Home > Mobile >  I need help putting values from one vector into another in R
I need help putting values from one vector into another in R

Time:10-07

I have two vectors in R

Vector 1 
0 0 0 0 0 0 0 0 0 0 
Vector 2 
1 1 3 1 1 1 1 1

I need to put the values from vector 2 into vector 1 but into specific positions so that vector 1 becomes

1 1 3 0 0 1 1 1 1 1

I need to do this in one line of code. I tried doing:

vector1[1:3,6:10] = vector2[1:3,4:8]

but I am getting the error "incorrect number of dimensions".

Is it possible to do this?

CodePudding user response:

vector1[c(1:3,6:10)] = vector2[c(1:3,4:8)]
> vector1
 [1] 1 1 3 0 0 1 1 1 1 1

CodePudding user response:

We may use negative indexing

vector1[-(4:5)] <-  vector2
vector1
 [1] 1 1 3 0 0 1 1 1 1 1
  •  Tags:  
  • r
  • Related