jacky_learns_to_code
jacky_learns_to_code

Reputation: 824

Looping a list and append current and next vector if conditions are met

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!

Upvotes: 1

Views: 69

Answers (3)

Rosalie Bruel
Rosalie Bruel

Reputation: 1493

Here is a solution:

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"))

# Get the 2nd element
m <- sapply(n_vectors, "[[", 2)
# Get the unique
u <- unique(m)

# Now, create the output
# First, create a new empty list
outlist <- list()

# Run through the list
for (l in seq_along(u)) {
    outlist <- c(outlist, list(unlist(n_vectors[m == u[l]])))
}

outlist
# [[1]]
# [1] "Hello Another World" "Key_123"             ""                    "Steven"
# [5] "Robert"
# 
# [[2]]
# [1] "Hello World" "Key_ABC"     "PART1"       "John"        "Paul"        "Hello World" "Key_ABC"
# [8] "PART2"       "Kim"         "Tim"
# 
# [[3]]
# [1] "Bye World" "Key_456"   ""          "Mo"        "Jordan"

Upvotes: 1

det
det

Reputation: 5232

library(purrr)

split(n_vectors, n_vectors %>% map_chr(2) %>% match(unique(.))) %>% map(reduce, c)

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389055

Use sapply to extract every 2nd element and use tapply to combine the common ones.

tapply(n_vectors, sapply(n_vectors, `[`, 2), unlist)

#$Key_123
#[1] "Hello Another World" "Key_123"             ""                   
#[4] "Steven"              "Robert"             

#$Key_456
#[1] "Bye World" "Key_456"   ""          "Mo"        "Jordan"   

#$Key_ABC
# [1] "Hello World" "Key_ABC"     "PART1"       "John"        "Paul"       
# [6] "Hello World" "Key_ABC"     "PART2"       "Kim"         "Tim"  

Upvotes: 1

Related Questions