Skip to content
Prev 257807 / 398502 Next

simple loop questions

On Apr 23, 2011, at 6:31 AM, <xueke at pdx.edu> wrote:

            
Assuming this to be in a data.frame named dat:

 > dat$A[which(dat$B==2) ]
[1] 1 2 6
 > dat$A[which(dat$B==4) ]
[1] 3 5

Results of varying lengths generally need to be returned in list form:

 > sapply(c(2,4), function(x) dat$A[which(dat$B==x) ] )
[[1]]
[1] 1 2 6

[[2]]
[1] 3 5
David Winsemius, MD
West Hartford, CT