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

前端 未结 2 1261
时光取名叫无心
时光取名叫无心 2020-12-01 17:23

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:

2条回答
  •  一个人的身影
    2020-12-01 17:45

    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)
    

提交回复
热议问题