Home > OS >  How to concatenate two lists of strings based on a shared index, with no spaces
How to concatenate two lists of strings based on a shared index, with no spaces

Time:06-24

I am working in R. Say I have two separate lists: a <- ["Cat", "Dog", "Mouse"] b <- ["One", Two", "Three"].

My desired output would be list c, ["CatOne", "DogTwo", "MouseThree"]

I know that using a combination of gsub and paste I can accomplish this when dealing with two strings, but I do not know how to accomplish this when dealing with a list of strings. Any help would be greatly appreciated.

CodePudding user response:

Using paste0.

paste0(a, b)
# [1] "CatOne"     "DogTwo"     "MouseThree"

Data:

a <- list("Cat", "Dog", "Mouse"); b <- list("One", "Two", "Three")

CodePudding user response:

Using str_c from stringr:

library(stringr)

str_c(a, b)

# [1] "CatOne"     "DogTwo"     "MouseThree"

Or with stringi:

library(stringi)

stri_paste(a, b)

Data

a <- list("Cat", "Dog", "Mouse")
b <- list("One", "Two", "Three")
  • Related