Get object name inside lapply

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Get object name inside lapply

R help mailing list-2
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  |  [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Get object name inside lapply

BenTupper
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 <[hidden email]> 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  |  [hidden email]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Ben Tupper
Bigelow Laboratory for Ocean Science
Reply | Threaded
Open this post in threaded view
|

Re: Get object name inside lapply

Aashish Jain-2
In reply to this post by R help mailing list-2
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 <
[hidden email]> 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  |  [hidden email]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Get object name inside lapply

Uwe Ligges-3
In reply to this post by R help mailing list-2


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  |  [hidden email]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Get object name inside lapply

Huzefa Khalil
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 <[hidden email]
> 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  |  [hidden email]
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Get object name inside lapply

Rolf Turner
On 26/02/16 11:37, Huzefa Khalil wrote:
> If you want the object names, you should use lapply over the names:
>
> lapply(names(c), function(x) {c[[x]]})

This makes no sense at all to me; you get the same result simply by
typing "c".

If the OP's message is interpreted literally, all he needs to do is type
"names(c)".  But this may not be what he *really* wants.  It's hard to tell.

Note that "c" is *not* a good name for a data set, since it is the name
of an (important) base function.

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Get object name inside lapply

Peter Dalgaard-2
In reply to this post by BenTupper

On 25 Feb 2016, at 22:43 , Ben Tupper <[hidden email]> 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 <[hidden email]> 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  |  [hidden email]
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.