Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6617347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:41:36+00:00 2026-05-25T20:41:36+00:00

In the process of (mostly) answering this question, I stumbled across something that I

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:41:37+00:00Added an answer on May 25, 2026 at 8:41 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a process something like a workflow, assume it like this: Prepare Eat
I’m in the process of developing pretty basic web application, that is mostly so
Looking to develop server-side application that will process documents. The source documents are mostly
I have a lot of text that I need to process for valid URLs.
I have a method that needs to process an incoming sequence of commands and
We have a REST service that calls out to a system process that could
This is mostly an implementation question for a webapp I'm trying to develop. I'm
I use wmic mostly as a linux-ps-equivalent in this way: wmic process where (name=java.exe)
I process a lot of text/data that I exchange between Python, R, and sometimes
I have a process which has a notify method which receives as a parameter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.