Re: [R] with lapply() how can you retrieve the name of the object

From: Andrew Yee <yee_at_post.harvard.edu>
Date: Mon, 21 Jul 2008 23:18:57 -0400

Thanks everyone for your suggestions (and sorry for the delay in the acknowledgement). Jorge and Jim, thanks for pointing out your approach.

Andrew

On Fri, Jul 18, 2008 at 7:02 PM, Jorge Ivan Velez <jorgeivanvelez_at_gmail.com>wrote:

>
> Dear Andrew,
>
> Following Jim Holtman' solution (thanks for pointed it out), try:
>
> a <- data.frame(var.1 = 1:5)
> b <- data.frame(var.1 = 11:15)
> test.list <- list(a=a, b=b)
> do.call(rbind,lapply(names(test.list), function(x){
> cbind(test.list[[x]], var.2=x)
> }))
>
>
> HTH,
>
> Jorge
>
>
>
>
> On Fri, Jul 18, 2008 at 6:57 PM, jim holtman <jholtman_at_gmail.com> wrote:
>
>> Is this what you wanted; you would use the 'names' to get the names in
>> the lapply:
>>
>> > lapply(names(test.list), function(x){
>> + cbind(test.list[[x]], var.2=x)
>> + })
>> [[1]]
>> var.1 var.2
>> 1 1 a
>> 2 2 a
>> 3 3 a
>> 4 4 a
>> 5 5 a
>>
>> [[2]]
>> var.1 var.2
>> 1 11 b
>> 2 12 b
>> 3 13 b
>> 4 14 b
>> 5 15 b
>>
>>
>>
>> On Fri, Jul 18, 2008 at 6:31 PM, Jorge Ivan Velez
>> <jorgeivanvelez_at_gmail.com> wrote:
>> > Dear Andrew,
>> >
>> > It's not the best solution but it does what you need:
>> >
>> > a <- data.frame(var.1 = 1:5)
>> > b <- data.frame(var.1 = 11:15)
>> > test.list <- list(a=a, b=b)
>> > res=do.call(rbind,test.list)
>> > res$var.2=substr(rownames(res),1,1)
>> > rownames(res)=NULL
>> > res
>> >
>> >
>> > HTH,
>> >
>> > Jorge
>> >
>> >
>> > On Fri, Jul 18, 2008 at 6:21 PM, Andrew Yee <yee_at_post.harvard.edu>
>> wrote:
>> >
>> >> In the following code, I'd like to be able to create a new variable
>> >> containing the value of the names of the list.
>> >>
>> >>
>> >> a <- data.frame(var.1 = 1:5)
>> >> b <- data.frame(var.1 = 11:15)
>> >>
>> >> test.list <- list(a=a, b=b)
>> >>
>> >> # in this case, names(test.list) is "a" and "b"
>> >>
>> >> # and I'd like to use lapply() so that
>> >> # I get something that looks like
>> >> # var.1 var.2
>> >> # 1 a
>> >> # 2 a
>> >> # 3 a
>> >> #etc.
>> >>
>> >> new.list <- lapply(test.list, function(x) {x$var.2 <- names(x)
>> >> x} )
>> >>
>> >>
>> >> # the above clearly doesn't do it. How do you pull out the names of
>> the
>> >> thing that is being lapplied?
>> >>
>> >> Thanks,
>> >> Andrew
>> >>
>> >> [[alternative HTML version deleted]]
>> >>
>> >> ______________________________________________
>> >> R-help_at_r-project.org mailing list
>> >> 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]]
>> >
>> > ______________________________________________
>> > R-help_at_r-project.org mailing list
>> > 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.
>> >
>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem you are trying to solve?
>>
>
>

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
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. Received on Tue 22 Jul 2008 - 03:29:38 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 22 Jul 2008 - 07:32:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive