I have this list of vectors n_vectors:
n_vectors
[[1]]
[1] "Hello Another World" "Key_123" "" "Steven" "Robert"
[[2]]
[1] "Hello World" "Key_ABC" "PART1" "John" "Paul"
[[3]]
[1] "Hello World" "Key_ABC" "PART2" "Kim" "Tim"
[[4]]
[1] "Bye World" "Key_456" "" "Mo" "Jordan"
> dput(n_vectors)
list(c("Hello Another World", "Key_123", "", "Steven", "Robert"
), c("Hello World", "Key_ABC", "PART1", "John", "Paul"), c("Hello World",
"Key_ABC", "PART2", "Kim", "Tim"), c("Bye World", "Key_456",
"", "Mo", "Jordan"))
How can I append if the second element within the list matches and append them to the list? For example [[2] and [[3]] matches with Key_ABC in the second element, then we will append [[2]] and [[3]] while [[1]] and [[4]] remain as it is.
The list can contain n number of vectors.
Expected outcome:
[[1]]
[1] "Hello Another World" "Key_123" "" "Steven" "Robert"
[[2]]
[1] "Hello World" "Key_ABC" "PART1" "John" "Paul" "Hello World" "Key_ABC" "PART2" "Kim"
[10] "Tim"
[[3]]
[1] "Bye World" "Key_456" "" "Mo" "Jordan"
Using a for loop resulted in a subscript out of bounds error. How can we effectively check the element of current and the next vector?
Any help would be much appreciated!