0

I have list which have this structure

ex1.
[[1]]
[1]"blah blah~"

ex2.
[[1]]
[1]"blah blah~~~"
[2]"fmlafmlaf~~"

ex3.
[[1]]
[1]"blaaaa~"

When I use paste function, ex1, ex3 make the outcome tidy. like below.

[[1]]
[1]"~~~sth"

However, In example 2, it has 2 components in one big list and the outcome of paste function is sth like dirty. I mean

[[1]]
[1] "c(\ "blah blah~~~" ...

So suddenly "c(\ ..~ , \n" appears and make my outcome dirty.

Even more, sometimes the paste function didn't work and the components didn't mixed.

What should I do?

user13232877
  • 205
  • 1
  • 9
  • 1
    can you put a reproducible code ? – Jrm_FRL Apr 27 '20 at 07:47
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please share a `dput()` of your data rather than just the console output. Please show exactly how you are calling the `paste()` function. What exactly do you want the result to be. You don't seem to be using `ex2` at all so what's the relevance? – MrFlick Apr 27 '20 at 07:47

3 Answers3

0

Don't use paste directly since you have a list use sapply

sapply(list_ex, paste0, collapse = "")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

If the purpose is to paste all examples into a large string then you can use unlist:

ex1 <- "blah blah~"
ex2 <- c("blah blah~", "blah ~ .... ~~~~")
list_ex <- list(ex1,ex2)

paste0(unlist(list_ex), collapse = " ")
[1] "blah blah~ blah blah~ blah ~ .... ~~~~"
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
0

An option with map

library(stringr)
library(purrr)
map_chr(list_ex, str_c, collapse="")
akrun
  • 874,273
  • 37
  • 540
  • 662