[R] fast subsetting of lists in lists

2010-12-07 Thread Alexander Senger
Hello, my data is contained in nested lists (which seems not necessarily to be the best approach). What I need is a fast way to get subsets from the data. An example: test - list(list(a = 1, b = 2, c = 3), list(a = 4, b = 5, c = 6), list(a = 7, b = 8, c = 9)) Now I would like to have all

Re: [R] fast subsetting of lists in lists

2010-12-07 Thread Gabor Grothendieck
On Tue, Dec 7, 2010 at 9:47 AM, Alexander Senger sen...@physik.hu-berlin.de wrote: Hello, my data is contained in nested lists (which seems not necessarily to be the best approach). What I need is a fast way to get subsets from the data. An example: test - list(list(a = 1, b = 2, c = 3),

Re: [R] fast subsetting of lists in lists

2010-12-07 Thread Gerrit Eichner
Hello, Alexander, does utest - unlist(test) utest[ names( utest) == a] come close to what you need? Hth, Gerrit On Tue, 7 Dec 2010, Alexander Senger wrote: Hello, my data is contained in nested lists (which seems not necessarily to be the best approach). What I need is a fast way to

Re: [R] fast subsetting of lists in lists

2010-12-07 Thread Alexander Senger
tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Alexander Senger Sent: Tuesday, December 07, 2010 9:12 AM To: r-help@r-project.org Subject: Re: [R] fast subsetting of lists in lists Hello Gerrit, Gabor, thank you

Re: [R] fast subsetting of lists in lists

2010-12-07 Thread Gabor Grothendieck
On Tue, Dec 7, 2010 at 12:12 PM, Alexander Senger sen...@physik.hu-berlin.de wrote: Hello Gerrit, Gabor, thank you for your suggestion. Unfortunately unlist seems to be rather expensive. A short test with one of my datasets gives 0.01s for an extraction based on my approach and 5.6s for

Re: [R] fast subsetting of lists in lists

2010-12-07 Thread Henrik Bengtsson
First, subset 'test' once, e.g. testT - test[1:3]; and then use sapply() on that, e.g. val - sapply(testT, FUN=function (x) { x$a }) Then you can avoid one level of function calls, by val - sapply(testT, FUN=[[, a) Second, there is some overhead in [[, $ etc. You can use .subset2() to avoid

Re: [R] fast subsetting of lists in lists

2010-12-07 Thread Matthew Dowle
Hello Alex, Assuming it was just an inadequate example (since a data.frame would suffice in that case), did you know that a data.frames' columns do not have to be vectors but can be lists? I don't know if that helps. DF = data.frame(a=1:3) DF$b = list(pi, 2:3, letters[1:5]) DF a

Re: [R] fast subsetting of lists in lists

2010-12-07 Thread Alexander Senger
Hello, Matthew's hint is interesting: Am 07.12.2010 19:16, schrieb Matthew Dowle: Hello Alex, Assuming it was just an inadequate example (since a data.frame would suffice in that case), did you know that a data.frames' columns do not have to be vectors but can be lists? I don't know if

Re: [R] fast subsetting of lists in lists

2010-12-07 Thread Alexander Senger
Hello Gerrit, Gabor, thank you for your suggestion. Unfortunately unlist seems to be rather expensive. A short test with one of my datasets gives 0.01s for an extraction based on my approach and 5.6s for unlist alone. The reason seems to be that unlist relies on lapply internally and does so

Re: [R] fast subsetting of lists in lists

2010-12-07 Thread Bert Gunter
Alexander: I'm not sure exactly what you want, so the following may be irrelevant... BUT, noting that data frames ARE lists and IF what you have can then be abstracted as lists of lists of lists of ... to various depths AND IF what you want is just to pick out and combined all named vectors

Re: [R] fast subsetting of lists in lists

2010-12-07 Thread Alexander Senger
Hi Bert, thank you for your suggestion. I'm sure it's a good one. But my intention in first place was to learn about getting subsets of list nested in lists the fast way (and preferably also the easy way, but that is only my laziness). It seems this thread is getting a bit long and also