[R] Get object name inside lapply

Aashish Jain aajains at gmail.com
Thu Feb 25 22:54:24 CET 2016


Why don't you simply use names(c) to get the names of all objects? If, for
your purposes, you still want the "names" function inside lapply, you can
use the following:
unlist(lapply(1:length(c), function(x) names(c[x])))
This will produce exactly same output as names(c) would. When you use
lapply(c, names) then it basically does this: names(c[[1]]) for first
member of the list, and that results to NULL. However, names(c[1]) gives
you "a".

Hope that helps.

On Thu, Feb 25, 2016 at 3:27 PM, Mohammad Tanvir Ahamed via R-help <
r-help at r-project.org> wrote:

> Hello,
>
> I want to get object name of a list inside lapply
>
> > c<-list(a=seq(1:5),b=seq(10:20))
> > lapply(c,names)
> $a
> NULL
>
> $b
> NULL
>
> Why NULL ?
>
> but i am expecting the names of object . Any help will be appreciated .
>
> I want to grab the names of object inside lapply for further process.
>
> Thanks .
>
>
> Tanvir Ahamed
> Göteborg, Sweden  |  mashranga at yahoo.com
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]



More information about the R-help mailing list