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. ______________________________________________ R-help@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.