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
Hi, Using your example (note I called the list 'z')... z <-list(a = seq(1:5), b = seq(10:20)) I picture lapply as extracting each element of z like this z[[i]] - the `[[` extracts the ith value from the context of residing in a list - hence it's name is 'lost' in the new context. That's different than z[i] which extracts a list of elements. Try.. z[['a']] vs. z['a'] As an alternative and depending upon what you really want to do, you could iterate through the names of the list, and pass the list as a parameter. r <- lapply(names(z), function(nm, dat = NULL){ sprintf("%s has %i elements", nm, length(dat[[nm]]) ) }, dat = z) r [[1]] [1] "a has 5 elements" [[2]] [1] "b has 11 elements" Ben> On Feb 25, 2016, at 4: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.
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]]
On 25.02.2016 22:27, Mohammad Tanvir Ahamed via R-help 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 ?Why should it? seq(1:5) has no names, nor has seq(10:20). Best, Uwe Ligges> > 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. >
If you want the object names, you should use lapply over the names: lapply(names(c), function(x) {c[[x]]}) On Thu, Feb 25, 2016 at 5:25 PM, Uwe Ligges <ligges at statistik.tu-dortmund.de> wrote:> > > On 25.02.2016 22:27, Mohammad Tanvir Ahamed via R-help 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 ? >> > > Why should it? seq(1:5) has no names, nor has seq(10:20). > > Best, > Uwe Ligges > > > > > > >> 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. >> >> > ______________________________________________ > 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]]
On 25 Feb 2016, at 22:43 , Ben Tupper <ben.bighair at gmail.com> wrote:> Hi, > > Using your example (note I called the list 'z')... > > z <-list(a = seq(1:5), b = seq(10:20)) > > I picture lapply as extracting each element of z like this z[[i]] - the `[[` extracts the ith value from the context of residing in a list - hence it's name is 'lost' in the new context. That's different than z[i] which extracts a list of elements. Try.. > > z[['a']] vs. z['a'] >Exactly. lapply applies the function to each _element_ in turn, not to a list containing one element. To further understand why the names do not carry onto elements, contemplate things like z <- list(a=c(b=1)) names(z[[1]])> As an alternative and depending upon what you really want to do, you could iterate through the names of the list, and pass the list as a parameter. > > r <- lapply(names(z), > function(nm, dat = NULL){ > sprintf("%s has %i elements", nm, length(dat[[nm]]) ) > }, > dat = z) > r > [[1]] > [1] "a has 5 elements" > > [[2]] > [1] "b has 11 elements" >Or,> mapply(z,names(z), FUN=function(e,n) sprintf("%s has %i elements", n, length(e)))a b "a has 5 elements" "b has 11 elements" and variation thereof. -pd> Ben > >> On Feb 25, 2016, at 4: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. > > ______________________________________________ > 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com