Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

r - Why this behavior when coercing a list to character via as.character()?

In the process of (mostly) answering this question, I stumbled across something that I feel like I really should already have seen before. Let's say you've got a list:

l <- list(a = 1:3, b = letters[1:3], c = runif(3))

Attempting to coerce l to various types returns an error:

> as.numeric(l)
Error: (list) object cannot be coerced to type 'double'
> as.logical(l)
Error: (list) object cannot be coerced to type 'logical'

However, I'm apparently allowed to coerce a list to character, I just wasn't expecting this result:

> as.character(l)
[1] "1:3"                                                        
[2] "c("a", "b", "c")"                                     
[3] "c(0.874045701464638, 0.0843329173512757, 0.809434881201014)"

Rather, if I'm allowed to coerce lists to character, I would have thought I'd see behavior more like this:

> as.character(unlist(l))
[1] "1"                  "2"                  "3"                  "a"                  "b"                 
[6] "c"                  "0.874045701464638"  "0.0843329173512757" "0.809434881201014"

Note that how I specify the list elements originally affects the output of as.character:

l <- list(a = c(1,2,3), b = letters[1:3], c = runif(3))
> as.character(l)
[1] "c(1, 2, 3)"                                                 
[2] "c("a", "b", "c")"                                     
[3] "c(0.344991483259946, 0.0492411875165999, 0.625746068544686)"

I have two questions:

  1. How is as.character dredging up the information from my original creation of the list l in order to spit out 1:3 versus c(1,2,3).
  2. In what circumstances would I want to do this, exactly? When would I want to call as.character() on a list and get output of this form?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For non-trivial lists, as.character uses deparse to generate the strings.

  1. Only if the vector is integer and 1,2,3,...,n - then it deparses as 1:n.

    c(1,2,3) is double whereas 1:3 is integer...

  2. No idea :-)

...but look at deparse if you want to understand as.character here:

deparse(c(1L, 2L, 3L)) # 1:3
deparse(c(3L, 2L, 1L)) # c(3L, 2L, 1L)
deparse(c(1, 2, 3))    # c(1, 2, 3)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...