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

  • Home
  • SEARCH
  • 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 8771003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:40:44+00:00 2026-06-13T17:40:44+00:00

I am vexed by the way R dynamically creates lists and I am hoping

  • 0

I am vexed by the way R dynamically creates lists and I am hoping someone can help me understand what is going on and what to do to fix my code. My problem is that for assignments of a vector of length one, a named vector is assigned, but assignments of a vector of length greater than one, a list is assigned. My desired outcome is that a list is assigned no matter the length of the vector I am assigning. How do I achieve such a result?

For example,

types <- c("a", "b")
lst <- vector("list", length(types))
names(lst) <- types
str(lst)
List of 2
 $ a: NULL
 $ b: NULL
lst$a[["foo"]] <- "hi"
lst$b[["foo"]] <- c("hi", "SO")
str(lst)
List of 2
 $ a: Named chr "hi"
  ..- attr(*, "names")= chr "foo"
 $ b:List of 1
  ..$ foo: chr [1:2] "hi" "SO"
str(lst$a)
 Named chr "hi"
 - attr(*, "names")= chr "foo"
str(lst$b)
List of 1
 $ foo: chr [1:2] "hi" "SO"

What I want to have as the outcome is a data structure that looks like this.

List of 2
 $ a:List of 1
  ..$ foo: chr [1] "hi"
 $ b:List of 1
  ..$ foo: chr [1:2] "hi" "SO"
  • 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-06-13T17:40:46+00:00Added an answer on June 13, 2026 at 5:40 pm

    While I also find it surprising, it is documented in ?[[:

    Recursive (list-like) objects:

     [...]
    
     When ‘$<-’ is applied to a ‘NULL’ ‘x’, it first coerces ‘x’ to
     ‘list()’.  This is what also happens with ‘[[<-’ if the
     replacement value ‘value’ is of length greater than one: if
     ‘value’ has length 1 or 0, ‘x’ is first coerced to a zero-length
     vector of the type of ‘value’.
    

    To override that behavior, you could specifically create empty lists before dynamically assigning to them:

    lst$a <- list()
    lst$b <- list()
    

    or like Josh suggested below, replace your lst <- vector("list", length(types)) with lst <- replicate(length(types), list()).

    Now that ‘x’ (lst$a or lst$b) is not ‘NULL’ but an empty list, your code should work as you expected:

    lst$a[["foo"]] <- "hi"
    lst$b[["foo"]] <- c("hi", "SO")
    str(lst)
    # List of 2
    #  $ a:List of 1
    #   ..$ foo: chr "hi"
    #  $ b:List of 1
    #   ..$ foo: chr [1:2] "hi" "SO"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a problem that has vexed me for a number of years. The
I may bounce my head off the wall shortly, I can't believe that something
I have a class which load and play a flv video file. My problem
I have a logical statement that says If everyone plays the game, we will
I call an action helper in one of my views using the following code
We have a rather large DB schema that is ocnstantly changing - every application
This may seem a simple question, but for some reason I am vexed. I
Vexed is a popular puzzle game, with many versions available (some of them GPL
I have a client WordPress website that allows users to upload custom artwork from
I use pie.js (http://css3pie.com/) a library that allows me to have css3 on ie6+.

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.