Similar questions have been asked here and here. But they don't specifically help my issue.
I have a large list of numerical vectors. What I'm trying to do is check if the sequence 1, 1, 1
appears anywhere in my list. And if it does appear, change it to 1, 1, 3
.
For example, if I have a list that looks like this:
myList <- list(c(1,1,1,2,3,5),
c(1,1,2),
c(1,2,3,4),
c(1,1,1,5,8))
We can see that the sequence 1, 1, 1
appears in myList[[1]]
and myList[[4]]
. Im trying to check each element of myList
, find that sequence and then change the 3rd instance of the number 1 to a number 3. In this example, my desired output would look like:
[[1]]
[1] 1 1 3 2 3 5
[[2]]
[1] 1 1 2
[[3]]
[1] 1 2 3 4
[[4]]
[1] 1 1 3 5 8
Any suggestions as to how I could achieve this?