Search code examples
rlistappend

Add named vector to a list


Suppose I have a list of variable entries:

lst <- list(a = 1:4, b = rep('k', 5), c = 3)

If I want to add a vector to this with a specified name I should be able to do so by:

c(f = 1:5, lst)

But instead of creating an entry called 'f' containing 1 2 3 4 5 it creates five entries (f1 - f5) containing one of the numbers each.

How do I supress this behavior?

I know I can use

lst$f <- 1:5

but I would like to append the list within a function call...


Solution

  • Turn f into a list of one, and then concatenate it:

    c(list(f = 1:5), lst)