Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you paste list of items in R

Tags:

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
like image 394
MAPK Avatar asked Dec 11 '15 15:12

MAPK


People also ask

How do I paste all elements into a vector in R?

You can use the paste() and paste0() functions in R to concatenate elements of a vector into a single string. The paste() function concatenates strings using a space as the default separator. The paste0() function concatenates strings using no space as the default separator.

How do you paste in R programming?

How to use the paste() function in R? A simple paste() will take multiple elements as inputs and concatenate those inputs into a single string. The elements will be separated by a space as the default option. But you can also change the separator value using the 'sep' parameter.

How do I remove spaces from paste in R?

Use the paste() Function With sep="" to Remove Spaces When pasting strings, R adds a space in between. We can use sep="" to remove the added space.


1 Answers

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"
like image 139
Cath Avatar answered Nov 15 '22 04:11

Cath