Re: [R] second element of a list
Thank you both for your help! I was interested in how to get at either element specifically. Your solutions work fine though. If I need the first element its lapply(thelist,'[[',1) and if I need the second its lapply(thelist,'[[',1). Thanks again, you just saved me a slow for loop! Kind regards, Greg On Sep 30, 2010, at 7:39 PM, Phil Spector wrote: > Gregory - > I'm confused -- if the first element is the matrix you > want, why would you use 2 as an index? > Here's a way to get a list with the first elements of each > member of a list: > > lapply(thelist,'[[',1) > > - Phil Spector >Statistical Computing Facility >Department of Statistics >UC Berkeley >spec...@stat.berkeley.edu > > > On Thu, 30 Sep 2010, Gregory Ryslik wrote: > >> Hi, >> >> I have a list of of n elements (where n is unknown beforehand). Each element >> of the list has two subelements. The first is a matrix, the second is a >> number. I want to make a list of just the matrices. >> >> I want to do something like mylists[[ ,2]] but that obviously doesn't work. >> Is there a simple way to get around this? >> >> Thanks! >> __ >> R-help@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. >> __ R-help@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.
Re: [R] second element of a list
Gregory - I'm confused -- if the first element is the matrix you want, why would you use 2 as an index? Here's a way to get a list with the first elements of each member of a list: lapply(thelist,'[[',1) - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spec...@stat.berkeley.edu On Thu, 30 Sep 2010, Gregory Ryslik wrote: Hi, I have a list of of n elements (where n is unknown beforehand). Each element of the list has two subelements. The first is a matrix, the second is a number. I want to make a list of just the matrices. I want to do something like mylists[[ ,2]] but that obviously doesn't work. Is there a simple way to get around this? Thanks! __ R-help@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. __ R-help@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.
Re: [R] second element of a list
Is the matrix the 1st or 2nd? Your description does not seem consistent. Here is one way to grab just the 2nd element from each sublist from a list: lapply( mylist, '[[', 2 ) -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > project.org] On Behalf Of Gregory Ryslik > Sent: Thursday, September 30, 2010 5:35 PM > To: R Help > Subject: [R] second element of a list > > Hi, > > I have a list of of n elements (where n is unknown beforehand). Each > element of the list has two subelements. The first is a matrix, the > second is a number. I want to make a list of just the matrices. > > I want to do something like mylists[[ ,2]] but that obviously doesn't > work. Is there a simple way to get around this? > > Thanks! > __ > R-help@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. __ R-help@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.