[R] strange list structure question

From: <markleeds_at_verizon.net>
Date: Mon, 03 Nov 2008 18:56:50 -0600 (CST)


my problem is more complex than below but I think below can suffice. i have a list and the name of it at the top level is GGG. so, if i do an lapply and operate on lower components in the sublist, then I can do as shown in EXAMPLE 1 and what will come back will be named GGG at the top level.

but, suppose that , the function inside the lapply function was more complex and i wanted to actually use "GGG" in the function but also return it at the top level. I can't figure a way to do both things:

  1. return the name GGG from the lapply

and

B) use the name GGG in the function that is called inside the lapply ?

Thanks for any suggestions on doing both ? I always think that I understand lists and then I always end up finding a new problem that i hadn't seen before.

# EXAMPLE ONE
dummylist <- list(list(x=1,y=2,z=3))

names(dummylist) <- "GGG"
print(dummylist)
print(str(dummylist))

# THIS RETURNS THE NAME GGG AT THE TOP LEVEL
one <- lapply(dummylist, function(.sublist) {

         lapply(1:3, function(.index) { # could use a for loop here or whatever. it doesn't matter

                .sublist[[.index]] + 2
              })
       })

print(one)

print(str(one))
print(names(one))

# EXAMPLE TWO

# THIS LETS ME USE THE NAME BUT THEN IT DOESN"T GET RETURNED
two <- lapply(1:length(dummylist[[1]]),function(.index) {

              temp <- dummylist[[1]][[.index]] + 2
              names(temp) <- names(dummylist)   # DUMB USAGE JUST TO 
SHOW USAGE
              temp
            })

print(two)

print(str(two))
print(names(two))

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 04 Nov 2008 - 01:01:57 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 04 Nov 2008 - 05:30:21 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