Hi how do I extract those components of a list that satisfy a certain requirement? If jj <- list(list(a=1,b=4:7),list(a=5,b=3:6),list(a=10,b=4:5)) I want just the components of jj that have b[1] ==4 which in this case would be the first and third of jj, viz list (jj[[1]],jj[[3]]). How to do this efficiently? My only idea was to loop through jj, and set unwanted components to NULL, but FAQ 7.1 warns against this. -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
maybe something like this: jj <- list(list(a = 1, b = 4:7), list(a = 5, b = 3:6), list(a = 10, b = 4:5)) ############### jj[sapply(jj, function(x) x$b[1] == 4)] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Robin Hankin" <r.hankin at noc.soton.ac.uk> To: "r-help" <R-help at stat.math.ethz.ch> Sent: Monday, June 13, 2005 4:23 PM Subject: [R] extracting components of a list> Hi > > how do I extract those components of a list that satisfy a certain > requirement? If > > jj <- list(list(a=1,b=4:7),list(a=5,b=3:6),list(a=10,b=4:5)) > > > I want just the components of jj that have b[1] ==4 which in this > case > would be the first and > third of jj, viz list (jj[[1]],jj[[3]]). > > How to do this efficiently? > > My only idea was to loop through jj, and set unwanted components to > NULL, but > FAQ 7.1 warns against this. > > > > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > R-help at 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 >
On Mon, 2005-06-13 at 15:23 +0100, Robin Hankin wrote:> Hi > > how do I extract those components of a list that satisfy a certain > requirement? If > > jj <- list(list(a=1,b=4:7),list(a=5,b=3:6),list(a=10,b=4:5)) > > > I want just the components of jj that have b[1] ==4 which in this case > would be the first and > third of jj, viz list (jj[[1]],jj[[3]]). > > How to do this efficiently? > > My only idea was to loop through jj, and set unwanted components to > NULL, but > FAQ 7.1 warns against this. >#Select the vectors named "b" from the elements of jj bvectors <- lapply(jj, FUN="[[", "b") #Take the first element of each b bfirst <- sapply(bvectors, head, 1) #Select elements of jj such that bfirst is 4 jj[bfirst == 4]
Dimitris, wouldn't this be more precise ---> sapply(jj,function(x) which(x$b[1]==4))[[1]] [1] 1 [[2]] numeric(0) [[3]] [1] 1 John Dimitris wrote --- maybe something like this: jj <- list(list(a = 1, b = 4:7), list(a = 5, b = 3:6), list(a = 10, b = 4:5)) ############### jj[sapply(jj, function(x) x$b[1] == 4)] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Robin Hankin" <r.hankin at noc.soton.ac.uk> To: "r-help" <R-help at stat.math.ethz.ch> Sent: Monday, June 13, 2005 4:23 PM Subject: [R] extracting components of a list> Hi > > how do I extract those components of a list that satisfy a certain > requirement? If > > jj <- list(list(a=1,b=4:7),list(a=5,b=3:6),list(a=10,b=4:5)) > > > I want just the components of jj that have b[1] ==4 which in this > case > would be the first and > third of jj, viz list (jj[[1]],jj[[3]]). > > How to do this efficiently? > > My only idea was to loop through jj, and set unwanted components to > NULL, but > FAQ 7.1 warns against this. > > > > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 >