Achal Neupane
Achal Neupane

Reputation: 5719

How do you paste list of items in R

How do you paste list of items and get the result as shown below?

 mylist<- list(c("PDLIM5", "CBG"), c("PDLIM5", "GTA"), "DDX60")

result

PDLIM5:CBG  PDLIM5:GTA  DDX60

Upvotes: 5

Views: 13929

Answers (2)

Sam Firke
Sam Firke

Reputation: 23014

The pipe-friendly tidyverse equivalent with a map function from purrr:

library(purrr)
mylist %>%
  map_chr(paste, collapse = ":")

Upvotes: 2

Cath
Cath

Reputation: 24074

you can try:

sapply(mylist, paste, collapse=":")
#[1] "PDLIM5:CBG" "PDLIM5:GTA" "DDX60"   

The result is a vector.

If you want to further paste the result, you can do:

paste(sapply(mylist, paste, collapse=":"), collapse=" ")
#[1] "PDLIM5:CBG PDLIM5:GTA DDX60"

Upvotes: 13

Related Questions