[R] subsets

Matthew Dowle mdowle at mdowle.plus.com
Mon Jan 24 00:32:42 CET 2011



require(data.table)
DT = as.data.table(df)

# 1. Patients with ah and ihd
DT[,.SD["ah"%in%diagnosis && "ihd"%in%diagnosis],by=id]

     id diagnosis
[1,]  2        ah
[2,]  2       ihd
[3,]  2        im
[4,]  4        ah
[5,]  4       ihd
[6,]  4    angina

# 2. Patients with ah but no ihd
DT[,.SD["ah"%in%diagnosis && !"ihd"%in%diagnosis],by=id]

     id diagnosis
[1,]  1        ah
[2,]  3        ah
[3,]  3    stroke


# 3. Patients with  ihd but no ah?
DT[,.SD[!"ah"%in%diagnosis && "ihd"%in%diagnosis],by=id]

     id diagnosis
[1,]  5       ihd
 
-- 
View this message in context: http://r.789695.n4.nabble.com/subsets-tp3227143p3233177.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list