[R] paste() turns list element character vector into deparsed expression. Why?
rub@k @end|ng |rom m@th@@@u@dk
Mon Nov 9 13:24:51 CET 2020
I think `paste()` just calls `as.character()` on each input argument
and then collapses things afterwards. Calling `as.character()` on the
first input argument generates exactly the output you show (and didn't
expect) and there is nothing to collapse. So changing `collapse = ""`
to anything else doesn't change behaviour.
The question is reduced to how `as.character()` should handle a list as
input. It seems to me that this input is so generic that it is hard to
handle graciously without all kinds of special cases. So you expect the
length one list
as.character(list(s = c("xyz", "uvw"))
to return the length 2 character vector `c("xyz", "uvw")`? What should
as.character(list(s = c("xyz", "uvw"), t = c("a", "b", "c"))
On Mon, 2020-11-09 at 11:38 +0000, Boris Steipe wrote:
> I was just surprised by very un-intuitive behaviour of paste(), which
> appears to collapse a one-column data frame or one-element list into
> a deparsed expression, rather than producing the expected string. Can
> someone kindly explain what's going on here?
> list(s = c("xyz", "uvw"))
> # s
> # 1 xyz
> # 2 uvw
> paste(list(s = c("xyz", "uvw")), collapse = "")
> #  "c(\"xyz\", \"uvw\")" # This is unexpected!
> I would have expected:
> #  "xyzuvw"
> ... which I do get with e.g.
> paste(list(s = c("xyz", "uvw"))$s, collapse = "")
> But what logic is there in returning a deparsed expression?
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help