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
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 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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With