[R] paste() turns list element character vector into deparsed expression. Why?

Ege Rubak 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"))

return?

Kind regards,
Ege

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?
> 
> 
> reprex:
> =======
> 
> list(s = c("xyz", "uvw"))
> #     s
> # 1 xyz
> # 2 uvw
> 
> paste(list(s = c("xyz", "uvw")), collapse = "")
> # [1] "c(\"xyz\", \"uvw\")"   # This is unexpected!
> 
> I would have expected:
> # [1] "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?
> 
> 
> 
> Thanks!
> Boris
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


More information about the R-help mailing list