Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add named vector to a list

Tags:

This is horribly basic but I can't seem to figure it out

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...

like image 569
ThomasP85 Avatar asked Jan 27 '12 10:01

ThomasP85


1 Answers

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

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

like image 131
Hong Ooi Avatar answered Sep 20 '22 06:09

Hong Ooi