I have a question regarding accessing the names of list components when applying a function with lapply. Here is an example that demonstrates what I'd like to do. I have a list like this one: mylist <- list(a=letters[1:10], b=letters[10:1], c=letters[1:3]) Now I would like to append the names of the list components to their corresponding vectors with the c() function. I thought this could be done like in the following command, but it doesn't: lapply(mylist, function(x) { c(names(x), x) } ) I know how to do this in a for loop, but lapply runs so much faster over large lists. Any help on this simple problem will be highly appreciated. Thomas -- Thomas Girke 1008 Noel T. Keen Hall University of California Riverside, CA 92521 Ph: 951-827-2469 Fax: 951-827-4437
Try this: lapply(names(mylist), function(nm) c(nm, mylist[[nm]])) On 3/29/06, Thomas Girke <thomas.girke at ucr.edu> wrote:> I have a question regarding accessing the names of list > components when applying a function with lapply. > > Here is an example that demonstrates what I'd like to do. > > I have a list like this one: > > mylist <- list(a=letters[1:10], b=letters[10:1], c=letters[1:3]) > > Now I would like to append the names of the list components to their > corresponding vectors with the c() function. I thought this could > be done like in the following command, but it doesn't: > > lapply(mylist, function(x) { c(names(x), x) } ) > > I know how to do this in a for loop, but lapply runs so much faster over > large lists. > > Any help on this simple problem will be highly appreciated. > > Thomas > > > -- > Thomas Girke > 1008 Noel T. Keen Hall > University of California > Riverside, CA 92521 > > Ph: 951-827-2469 > Fax: 951-827-4437 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
On Wed, 29 Mar 2006, Thomas Girke wrote:> I have a question regarding accessing the names of list > components when applying a function with lapply. > > Here is an example that demonstrates what I'd like to do. > > I have a list like this one: > > mylist <- list(a=letters[1:10], b=letters[10:1], c=letters[1:3]) > > Now I would like to append the names of the list components to their > corresponding vectors with the c() function. I thought this could > be done like in the following command, but it doesn't: > > lapply(mylist, function(x) { c(names(x), x) } ) > > I know how to do this in a for loop, but lapply runs so much faster over > large lists.Try lapply(names(mylist), function(x) c(x, mylist[[x]])) (that prepends, but your code attempts to prepend rather than append so I am guessing that it what you wanted). -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Kjetil Brinchmann Halvorsen
2006-Apr-19 20:32 UTC
[R] access list component names with lapply
Thomas Girke wrote:> I have a question regarding accessing the names of list > components when applying a function with lapply. > > Here is an example that demonstrates what I'd like to do. > > I have a list like this one: > > mylist <- list(a=letters[1:10], b=letters[10:1], c=letters[1:3])This is how I am doing this: mapply(functuion(x,y)myfun(x, y),x, names(x))) Kjetil> > Now I would like to append the names of the list components to their > corresponding vectors with the c() function. I thought this could > be done like in the following command, but it doesn't: > > lapply(mylist, function(x) { c(names(x), x) } ) > > I know how to do this in a for loop, but lapply runs so much faster over > large lists. > > Any help on this simple problem will be highly appreciated. > > Thomas > >