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

CS Sparks corey.sparks at utsa.edu
Fri Jun 24 15:55:16 CEST 2011


> 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
>



More information about the R-help mailing list