Reputation: 494
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!)
Upvotes: 1
Views: 5704
Reputation: 99331
While yes, cat
will concatenate and print to the console, it does not actually function in the same way paste
does. It's result best explained in help("cat")
The collapse
argument in paste
is effectively the opposite of the split
argument in strsplit
. And you can use sapply
to return the simplified pasted vector.
x <- c("on,e","tw,o","thre,e","fou,r")
( y <- sapply(strsplit(x, ","), paste, collapse = ",") )
# [1] "on,e" "tw,o" "thre,e" "fou,r"
( z <- vapply(strsplit(x, ","), paste, character(1L), collapse = ",") )
# [1] "on,e" "tw,o" "thre,e" "fou,r"
identical(x, y)
# [1] TRUE
identical(x, z)
# [1] TRUE
Note that for cases like this, vapply
will be more efficient than sapply
. And adding fixed = TRUE
in strsplit
should increase efficiency as well.
Upvotes: 6