Home > Back-end >  Is there a way to change an element of a sequence in R?
Is there a way to change an element of a sequence in R?

Time:10-11

I have tried to change the 11th element of a sequence (1 to 50, total length 1000). But I cannot seem to understand how to do it. I have googled it and so far haven't found an answer for it. Could someone give me an example as to how to complete this? As a side note the sequence I have is C=seq(1,50) and then D=rep(C,1000). Sorry if this is a nonsense question but I am really stuck with it at the moment,

thank you

CodePudding user response:

If you want the 11th of every 50, repeating, use indexing like this:

D[1:50==11]<-999

CodePudding user response:

You can use [<- or replace to change an element at a given position.

`[<-`(seq(1,50), 11, -5)
#replace(seq(1,50), 11, -5) #Alternative
#[1]  1  2  3  4  5  6  7  8  9 10 -5 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

where 11 is the position and -5 the new value at the given position.

  • Related