Re: [R] How to iteratively extract elements out of a list
Hi try to do it without loop lapply(m,function(x) x[x2]) HTH Petr On 25 Aug 2006 at 13:52, xpRt.wannabe wrote: Date sent: Fri, 25 Aug 2006 13:52:51 -0500 From: xpRt.wannabe [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Subject:[R] How to iteratively extract elements out of a list Dear List, The following code produces a list, which is what I what: set.seed(123) tmpf - function() { x - rpois(rpois(1,4),2) } n - 3 m - replicate(n,tmpf()) m [[1]] [1] 3 2 4 [[2]] [1] 0 2 4 2 2 5 2 [[3]] [1] 2 0 4 1 0 Now I need something that would to extract iteratively (or as many times as the size of 'n') the values that are greater than 2 in each component of 'm' into another list such that the sub-list would be: [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 Below is what I tried: for(i in 1:3) sub.list - lapply(m,subset,m[[i]]2) sub.list which gives me something different from what I want: [[1]] [1] 4 [[2]] [1] 4 [[3]] [1] 4 Any help would be appreciated. version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major2 minor2.1 year 2005 month12 day 20 svn rev 36812 language R __ R-help@stat.math.ethz.ch 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. Petr Pikal [EMAIL PROTECTED] __ R-help@stat.math.ethz.ch 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.
Re: [R] How to iteratively extract elements out of a list
sub.m - lapply(m, function(x)x[x2]) sub.m [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 sub.m[unlist(lapply(sub.m, function(x) length(x) == 2))] [[1]] [1] 3 4 [[2]] [1] 4 5 sub4.m - lapply(m, function(x)x[x4]) sub4.m[unlist(lapply(sub4.m, function(x) length(x) 0))] [[1]] [1] 5 Patrick Burns [EMAIL PROTECTED] +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and A Guide for the Unwilling S User) xpRt.wannabe wrote: Yet another question: Let's say I do the following: set.seed(123) tmpf - function(){ x - rpois(rpois(1,4),2) } n - 3 m - replicate(n, tmpf()) m sub.m - lapply(m, function(x)x[x2]) 'sub.m' gives me: [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 The question is: What do I need to do such that I can extract componets of length 2 in 'sub.m' into another sublist, which would look like this: [[1]] [1] 3 4 [[2]] [1] 4 5 I think that's all the questions I can think of -- for now. Many, many thanks!!! On 8/25/06, xpRt. wannabe [EMAIL PROTECTED] wrote: Jim and Patrick, Both of you made the same suggestion, which works great! A follow-up question: Suppose I change the condition 'x2' in 'lapply' to 'x4', as follows: set.seed(123) tmpf - function() { x - rpois(rpois(1,4),2) } n - 3 m - replicate(n,tmpf()) m sub.m - lapply(m, function(x)x[x4]) # was x2 As a result, I'd get: sub.m [[1]] numeric(0) [[2]] [1] 5 [[3]] numeric(0) However, what would I need to do such that 'sub.m' contains only the non-zero length component; namely, the 'sub.m[[2]]'? In essence, I'd like to drop all the components of zero length such that 'sub.m' results in: [[1]] [1] 5 My best effort was to use 'lapply' again: lapply(sub.m, function(x)x[length(x)0]) which still gives me: [[1]] numeric(0) [[2]] [1] 5 [[3]] numeric(0) Again, any help would be greately appreciated. p.s. Sorry to bug you again. I should have thought through a little more prior to composing an example that would represent all possible scenarios. On 8/25/06, jim holtman [EMAIL PROTECTED] wrote: try this: set.seed(123) tmpf - function() { + x - rpois(rpois(1,4),2) + } n - 3 m - replicate(n,tmpf()) m [[1]] [1] 3 2 4 [[2]] [1] 0 2 4 2 2 5 2 [[3]] [1] 2 0 4 1 0 lapply(m, function(x)x[x2]) [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 On 8/25/06, xpRt.wannabe [EMAIL PROTECTED] wrote: Dear List, The following code produces a list, which is what I what: set.seed(123) tmpf - function() { x - rpois(rpois(1,4),2) } n - 3 m - replicate(n,tmpf()) m [[1]] [1] 3 2 4 [[2]] [1] 0 2 4 2 2 5 2 [[3]] [1] 2 0 4 1 0 Now I need something that would to extract iteratively (or as many times as the size of 'n') the values that are greater than 2 in each component of 'm' into another list such that the sub-list would be: [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 Below is what I tried: for(i in 1:3) sub.list - lapply(m,subset,m[[i]]2) sub.list which gives me something different from what I want: [[1]] [1] 4 [[2]] [1] 4 [[3]] [1] 4 Any help would be appreciated. version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major2 minor2.1 year 2005 month12 day 20 svn rev 36812 language R __ R-help@stat.math.ethz.ch 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? __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] How to iteratively extract elements out of a list
lapply(m,function(x)x[x2]) [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 __ R-help@stat.math.ethz.ch 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.
Re: [R] How to iteratively extract elements out of a list
Thank you! On 8/26/06, Patrick Burns [EMAIL PROTECTED] wrote: sub.m - lapply(m, function(x)x[x2]) sub.m [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 sub.m[unlist(lapply(sub.m, function(x) length(x) == 2))] [[1]] [1] 3 4 [[2]] [1] 4 5 sub4.m - lapply(m, function(x)x[x4]) sub4.m[unlist(lapply(sub4.m, function(x) length(x) 0))] [[1]] [1] 5 Patrick Burns [EMAIL PROTECTED] +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and A Guide for the Unwilling S User) xpRt.wannabe wrote: Yet another question: Let's say I do the following: set.seed(123) tmpf - function(){ x - rpois(rpois(1,4),2) } n - 3 m - replicate(n, tmpf()) m sub.m - lapply(m, function(x)x[x2]) 'sub.m' gives me: [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 The question is: What do I need to do such that I can extract componets of length 2 in 'sub.m' into another sublist, which would look like this: [[1]] [1] 3 4 [[2]] [1] 4 5 I think that's all the questions I can think of -- for now. Many, many thanks!!! On 8/25/06, xpRt. wannabe [EMAIL PROTECTED] wrote: Jim and Patrick, Both of you made the same suggestion, which works great! A follow-up question: Suppose I change the condition 'x2' in 'lapply' to 'x4', as follows: set.seed(123) tmpf - function() { x - rpois(rpois(1,4),2) } n - 3 m - replicate(n,tmpf()) m sub.m - lapply(m, function(x)x[x4]) # was x2 As a result, I'd get: sub.m [[1]] numeric(0) [[2]] [1] 5 [[3]] numeric(0) However, what would I need to do such that 'sub.m' contains only the non-zero length component; namely, the 'sub.m[[2]]'? In essence, I'd like to drop all the components of zero length such that 'sub.m' results in: [[1]] [1] 5 My best effort was to use 'lapply' again: lapply(sub.m, function(x)x[length(x)0]) which still gives me: [[1]] numeric(0) [[2]] [1] 5 [[3]] numeric(0) Again, any help would be greately appreciated. p.s. Sorry to bug you again. I should have thought through a little more prior to composing an example that would represent all possible scenarios. On 8/25/06, jim holtman [EMAIL PROTECTED] wrote: try this: set.seed(123) tmpf - function() { + x - rpois(rpois(1,4),2) + } n - 3 m - replicate(n,tmpf()) m [[1]] [1] 3 2 4 [[2]] [1] 0 2 4 2 2 5 2 [[3]] [1] 2 0 4 1 0 lapply(m, function(x)x[x2]) [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 On 8/25/06, xpRt.wannabe [EMAIL PROTECTED] wrote: Dear List, The following code produces a list, which is what I what: set.seed(123) tmpf - function() { x - rpois(rpois(1,4),2) } n - 3 m - replicate(n,tmpf()) m [[1]] [1] 3 2 4 [[2]] [1] 0 2 4 2 2 5 2 [[3]] [1] 2 0 4 1 0 Now I need something that would to extract iteratively (or as many times as the size of 'n') the values that are greater than 2 in each component of 'm' into another list such that the sub-list would be: [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 Below is what I tried: for(i in 1:3) sub.list - lapply(m,subset,m[[i]]2) sub.list which gives me something different from what I want: [[1]] [1] 4 [[2]] [1] 4 [[3]] [1] 4 Any help would be appreciated. version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major2 minor2.1 year 2005 month12 day 20 svn rev 36812 language R __ R-help@stat.math.ethz.ch 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? __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] How to iteratively extract elements out of a list
On Sat, 26-Aug-2006 at 09:57AM +0100, Patrick Burns wrote: | sub.m - lapply(m, function(x)x[x2]) | sub.m | [[1]] | [1] 3 4 | | [[2]] | [1] 4 5 | | [[3]] | [1] 4 | | sub.m[unlist(lapply(sub.m, function(x) length(x) == 2))] | [[1]] | [1] 3 4 | | [[2]] | [1] 4 5 | | sub4.m - lapply(m, function(x)x[x4]) | sub4.m[unlist(lapply(sub4.m, function(x) length(x) 0))] | [[1]] | [1] 5 Or slightly shorter in this case: sub.m[sapply(sub.m, function(x) length(x) == 2)] etc. -- ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. ___Patrick Connolly {~._.~} Great minds discuss ideas _( Y )_Middle minds discuss events (:_~*~_:)Small minds discuss people (_)-(_) . Anon ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. __ R-help@stat.math.ethz.ch 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.
Re: [R] How to iteratively extract elements out of a list
On 8/26/06, Patrick Connolly [EMAIL PROTECTED] wrote: On Sat, 26-Aug-2006 at 09:57AM +0100, Patrick Burns wrote: | sub.m - lapply(m, function(x)x[x2]) | sub.m | [[1]] | [1] 3 4 | | [[2]] | [1] 4 5 | | [[3]] | [1] 4 | | sub.m[unlist(lapply(sub.m, function(x) length(x) == 2))] | [[1]] | [1] 3 4 | | [[2]] | [1] 4 5 | | sub4.m - lapply(m, function(x)x[x4]) | sub4.m[unlist(lapply(sub4.m, function(x) length(x) 0))] | [[1]] | [1] 5 Or slightly shorter in this case: sub.m[sapply(sub.m, function(x) length(x) == 2)] or even shorter: sub.m[sapply(sub.m, length) == 2] __ R-help@stat.math.ethz.ch 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.
Re: [R] How to iteratively extract elements out of a list
try this: set.seed(123) tmpf - function() { + x - rpois(rpois(1,4),2) + } n - 3 m - replicate(n,tmpf()) m [[1]] [1] 3 2 4 [[2]] [1] 0 2 4 2 2 5 2 [[3]] [1] 2 0 4 1 0 lapply(m, function(x)x[x2]) [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 On 8/25/06, xpRt.wannabe [EMAIL PROTECTED] wrote: Dear List, The following code produces a list, which is what I what: set.seed(123) tmpf - function() { x - rpois(rpois(1,4),2) } n - 3 m - replicate(n,tmpf()) m [[1]] [1] 3 2 4 [[2]] [1] 0 2 4 2 2 5 2 [[3]] [1] 2 0 4 1 0 Now I need something that would to extract iteratively (or as many times as the size of 'n') the values that are greater than 2 in each component of 'm' into another list such that the sub-list would be: [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 Below is what I tried: for(i in 1:3) sub.list - lapply(m,subset,m[[i]]2) sub.list which gives me something different from what I want: [[1]] [1] 4 [[2]] [1] 4 [[3]] [1] 4 Any help would be appreciated. version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major2 minor2.1 year 2005 month12 day 20 svn rev 36812 language R __ R-help@stat.math.ethz.ch 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? __ R-help@stat.math.ethz.ch 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.
Re: [R] How to iteratively extract elements out of a list
Jim and Patrick, Both of you made the same suggestion, which works great! A follow-up question: Suppose I change the condition 'x2' in 'lapply' to 'x4', as follows: set.seed(123) tmpf - function() { x - rpois(rpois(1,4),2) } n - 3 m - replicate(n,tmpf()) m sub.m - lapply(m, function(x)x[x4]) # was x2 As a result, I'd get: sub.m [[1]] numeric(0) [[2]] [1] 5 [[3]] numeric(0) However, what would I need to do such that 'sub.m' contains only the non-zero length component; namely, the 'sub.m[[2]]'? In essence, I'd like to drop all the components of zero length such that 'sub.m' results in: [[1]] [1] 5 My best effort was to use 'lapply' again: lapply(sub.m, function(x)x[length(x)0]) which still gives me: [[1]] numeric(0) [[2]] [1] 5 [[3]] numeric(0) Again, any help would be greately appreciated. p.s. Sorry to bug you again. I should have thought through a little more prior to composing an example that would represent all possible scenarios. On 8/25/06, jim holtman [EMAIL PROTECTED] wrote: try this: set.seed(123) tmpf - function() { + x - rpois(rpois(1,4),2) + } n - 3 m - replicate(n,tmpf()) m [[1]] [1] 3 2 4 [[2]] [1] 0 2 4 2 2 5 2 [[3]] [1] 2 0 4 1 0 lapply(m, function(x)x[x2]) [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 On 8/25/06, xpRt.wannabe [EMAIL PROTECTED] wrote: Dear List, The following code produces a list, which is what I what: set.seed(123) tmpf - function() { x - rpois(rpois(1,4),2) } n - 3 m - replicate(n,tmpf()) m [[1]] [1] 3 2 4 [[2]] [1] 0 2 4 2 2 5 2 [[3]] [1] 2 0 4 1 0 Now I need something that would to extract iteratively (or as many times as the size of 'n') the values that are greater than 2 in each component of 'm' into another list such that the sub-list would be: [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 Below is what I tried: for(i in 1:3) sub.list - lapply(m,subset,m[[i]]2) sub.list which gives me something different from what I want: [[1]] [1] 4 [[2]] [1] 4 [[3]] [1] 4 Any help would be appreciated. version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major2 minor2.1 year 2005 month12 day 20 svn rev 36812 language R __ R-help@stat.math.ethz.ch 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? __ R-help@stat.math.ethz.ch 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.
Re: [R] How to iteratively extract elements out of a list
Yet another question: Let's say I do the following: set.seed(123) tmpf - function(){ x - rpois(rpois(1,4),2) } n - 3 m - replicate(n, tmpf()) m sub.m - lapply(m, function(x)x[x2]) 'sub.m' gives me: [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 The question is: What do I need to do such that I can extract componets of length 2 in 'sub.m' into another sublist, which would look like this: [[1]] [1] 3 4 [[2]] [1] 4 5 I think that's all the questions I can think of -- for now. Many, many thanks!!! On 8/25/06, xpRt. wannabe [EMAIL PROTECTED] wrote: Jim and Patrick, Both of you made the same suggestion, which works great! A follow-up question: Suppose I change the condition 'x2' in 'lapply' to 'x4', as follows: set.seed(123) tmpf - function() { x - rpois(rpois(1,4),2) } n - 3 m - replicate(n,tmpf()) m sub.m - lapply(m, function(x)x[x4]) # was x2 As a result, I'd get: sub.m [[1]] numeric(0) [[2]] [1] 5 [[3]] numeric(0) However, what would I need to do such that 'sub.m' contains only the non-zero length component; namely, the 'sub.m[[2]]'? In essence, I'd like to drop all the components of zero length such that 'sub.m' results in: [[1]] [1] 5 My best effort was to use 'lapply' again: lapply(sub.m, function(x)x[length(x)0]) which still gives me: [[1]] numeric(0) [[2]] [1] 5 [[3]] numeric(0) Again, any help would be greately appreciated. p.s. Sorry to bug you again. I should have thought through a little more prior to composing an example that would represent all possible scenarios. On 8/25/06, jim holtman [EMAIL PROTECTED] wrote: try this: set.seed(123) tmpf - function() { + x - rpois(rpois(1,4),2) + } n - 3 m - replicate(n,tmpf()) m [[1]] [1] 3 2 4 [[2]] [1] 0 2 4 2 2 5 2 [[3]] [1] 2 0 4 1 0 lapply(m, function(x)x[x2]) [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 On 8/25/06, xpRt.wannabe [EMAIL PROTECTED] wrote: Dear List, The following code produces a list, which is what I what: set.seed(123) tmpf - function() { x - rpois(rpois(1,4),2) } n - 3 m - replicate(n,tmpf()) m [[1]] [1] 3 2 4 [[2]] [1] 0 2 4 2 2 5 2 [[3]] [1] 2 0 4 1 0 Now I need something that would to extract iteratively (or as many times as the size of 'n') the values that are greater than 2 in each component of 'm' into another list such that the sub-list would be: [[1]] [1] 3 4 [[2]] [1] 4 5 [[3]] [1] 4 Below is what I tried: for(i in 1:3) sub.list - lapply(m,subset,m[[i]]2) sub.list which gives me something different from what I want: [[1]] [1] 4 [[2]] [1] 4 [[3]] [1] 4 Any help would be appreciated. version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major2 minor2.1 year 2005 month12 day 20 svn rev 36812 language R __ R-help@stat.math.ethz.ch 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? __ R-help@stat.math.ethz.ch 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.