I'm trying to understand the behavior of strsplit
and paste
, which are inverse functions. However, when I strsplit
a vector, a list is returned, like so:
> strsplit(c("on,e","tw,o","thre,e","fou,r"),",")
[[1]]
[1] "on" "e"
[[2]]
[1] "tw" "o"
[[3]]
[1] "thre" "e"
[[4]]
[1] "fou" "r"
I tried using lapply
to cat
the elements of the list back together, but it doesn't work:
> lapply(strsplit(c("on,e","tw,o","thre,e","fou,r"),","),cat)
on etw othre efou r[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
[[4]]
NULL
The same formula with paste
instead of cat
actually does nothing at all! Why am I getting these results? and how can I get the result I want, which is the original vector back again?
(Obviously, in my actual code I'm trying to do more with the strsplit
and cat
than just return the original vector, but I think a solution to this problem will work for mine. Thanks!)
While yes,
cat
will concatenate and print to the console, it does not actually function in the same waypaste
does. It's result best explained inhelp("cat")
The
collapse
argument inpaste
is effectively the opposite of thesplit
argument instrsplit
. And you can usesapply
to return the simplified pasted vector.Note that for cases like this,
vapply
will be more efficient thansapply
. And addingfixed = TRUE
instrsplit
should increase efficiency as well.