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: A) 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))
Hi Rolf: it's not what's inside the lapplys that i was interested in which probably made my question more confusing. all i was trying to show was that, in EXAMPLE 1, the name comes back at the top level of the result. in EXAMPLE 2, i can use the name GGG inside the lapply functon to do whatever ( the whatever was just a stupid example which can be disregarded ). my question is : is it possible to do both: be able to return the name at the top level but also mess with the name inside the lapply. i'm ccing the list again because it's quite clear that my question was ill posed. thanks for that enlightenment. I can't show my actual example because it's too complicated but essentially i want to be able to return GGG at the top level like EXAMPLE 1 does but also use GGG inside the lapply like EXAMPLE 2 does. Thanks for telling me that i'm confusing !!!! On Mon, Nov 3, 2008 at 9:10 PM, Rolf Turner wrote:> Mark: > > What the <expletive deleted> are you actually trying to do? Your two > applications > of lapply() do in effect quite different things. The first gives a > list of > length 1; the single entry is named ``GGG''; this entry is a list of > length 3. > > The second application gives a list of length 3. Each entry of this > list is a vector > of length 1. The single entry of each such vector has name ``GGG'' > just as you requested. > > R does what you ask it to do. You have to be clear in your own mind > as to just > what you're asking. > > cheers, > > Rolf > > ###################################################################### > Attention:This e-mail message is privileged and confidential. If you > are not theintended recipient please delete the message and notify the > sender.Any views or opinions presented are solely those of the author. > > This e-mail has been scanned and cleared by > MailMarshalwww.marshalsoftware.com > ######################################################################
On 11/3/08, markleeds at verizon.net <markleeds at verizon.net> wrote:> 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: > > A) 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 > })Why not add names(two) <- names(dummylist) after this step? You cannot really expect lapply to magically figure out that it should take the names of the result from what is an essentially arbitrary object in the context of the lapply call. If you are happy with your first approach as long as you have the top-level name available inside your function, you could pass that as an extra argument: one <- lapply(dummylist, function(.sublist, .name) { lapply(1:3, function(.index) { temp <- .sublist[[.index]] + 2 names(temp) <- .name temp }) }, .name = names(dummylist)) -Deepayan