[R] Fwd: Extract element of a list based on an index value

John Kane jrkrideau at yahoo.ca
Fri Jun 24 17:09:10 CEST 2011


Is this doing what you want?  It's going from the raw data not the list

===================================================================


library(reshape2)

# sample data.frame
xx <-structure(list(Warrior = c(3793L, 2013L, 3769L, 2747L, 2083L,
2085L, 2086L, 2556L, 2050L, 2158L, 3045L), SibID = c(2013L, 2024L,
2024L, 2037L, 2039L, 2039L, 2039L, 2039L, 2040L, 2040L, 2040L
), birth.year = c(1926L, 1934L, 1918L, 1928L, 1944L, 1949L, NA,
1940L, 1950L, 1926L, 1948L)), .Names = c("Warrior", "SibID",
"birth.year"), class = "data.frame", row.names = c("1100", "4",
"1094", "632", "176", "187", "192", "495", "71", "343", "930"
))



(d1 <-melt(xx, id= c("SibID")))

dcast(d1, SibID ~ variable, min)

-----------------------------------------------------------------------

--- On Fri, 6/24/11, CS Sparks <corey.sparks at utsa.edu> wrote:

> From: CS Sparks <corey.sparks at utsa.edu>
> Subject: [R] Fwd: Extract element of a list based on an index value
> To: "R Help Help" <r-help at r-project.org>
> Received: Friday, June 24, 2011, 9:55 AM
> > Dear list,
> >
> > I have some data on a geneaology, here is a subset:
> > warmerge[1:11,c(1,6,25)]
> >     Warrior SibID birth.year
> > 1100    3793  2013   
>    1926
> > 4       2013  2024 
>      1934
> > 1094    3769  2024   
>    1918
> > 632     2747  2037 
>      1928
> > 176     2083  2039 
>      1944
> > 187     2085  2039 
>      1949
> > 192     2086  2039 
>        NA
> > 495     2556  2039 
>      1940
> > 71      2050  2040   
>    1950
> > 343     2158  2040 
>      1926
> > 930     3045  2040 
>      1948
> >
> > I have then extracted the lowest birth.year for each
> SibID using:
> >
> >
> br<-tapply(warmerge$birth.year,warmerge$SibID,which.min)
> >
> > br[1:5]
> > $`2013`
> > [1] 1
> >
> > $`2024`
> > [1] 2
> >
> > $`2037`
> > [1] 1
> >
> > $`2039`
> > [1] 4
> >
> > $`2040`
> > [1] 2
> >
> >
> > My goal now is to extract out the value of Warrior
> that corresponds  
> > to this index value
> >
> > I've been trying to extract out the actual value of
> the list  
> > corresponding to the index value, so for example, the
> fourth value  
> > of SibID I would like to return
> >
> > 2556
> >
> > and for the fifth value of SibID I would like
> >
> > 2158
> >
> > as these correspond to the index value. This wouldn't
> be so bad if  
> > it were 5 geneologies, but there are hundreds. I've
> been fighting  
> > with lapply() and sapply() to to avail, for instance I
> can turn the  
> > geneaology into a list using:
> >
> > crap2<-tapply(warmerge$Warrior, warmerge$SibID,
> "[")
> >
> > crap2[1:5]
> > $`2013`
> > [1] 3793
> >
> > $`2024`
> > [1] 2013 3769
> >
> > $`2037`
> > [1] 2747
> >
> > $`2039`
> > [1] 2083 2085 2086 2556
> >
> > $`2040`
> > [1] 2050 2158 3045
> >
> > lapply(crap2[1:5], "[", i=1)
> > $`2013`
> > [1] 3793
> >
> > $`2024`
> > [1] 2013
> >
> > $`2037`
> > [1] 2747
> >
> > $`2039`
> > [1] 2083
> >
> > $`2040`
> > [1] 2050
> >
> > gives me the first element of each list, but my
> question is how  
> > could I get something like this for a non-fixed i
> corresponding to  
> > my index?
> >
> > Any suggestions would be greatly appreciated.
> >
> > Best,
> > Corey
> >
> >
> > Corey Sparks
> > Assistant Professor
> > Department of Demography
> > College of Public Policy
> > 501 West Durango Blvd
> > Monterrey Building 2.270C
> > San Antonio, TX 78207
> > corey.sparks 'at' utsa.edu
> > 210 458 3166
> > Latitude: 29.423614  /  Longitude:
> -98.504282
> >
> 
> ______________________________________________
> 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.
>



More information about the R-help mailing list