Hi, Has anyone been able to figure out how to print all duplicated observations? I have a dataset, with patients ID, and other lab records. Some patients have multiple lab records, but 'duplicated' ID will only show me the duplicates, not the original observation. How can I print both the original one and the duplicates? Thanks
all duplicated wanted
3 messages · Weijia Wang, Peter Dalgaard, arun
On Aug 3, 2012, at 09:06 , Weijia Wang wrote:
Hi, Has anyone been able to figure out how to print all duplicated observations? I have a dataset, with patients ID, and other lab records. Some patients have multiple lab records, but 'duplicated' ID will only show me the duplicates, not the original observation. How can I print both the original one and the duplicates?
Something like this? dd[ID %in% unique(ID[duplicated(ID)]),] Let's try:
ID <- sample(1:10, 10, replace=TRUE) table(ID)
ID 1 2 3 4 7 10 1 1 3 1 2 2
ID[ID %in% unique(ID[duplicated(ID)])]
[1] 7 7 10 3 3 3 10 The unique() bit is really just for efficiency:
ID[ID %in% ID[duplicated(ID)]]
[1] 7 7 10 3 3 3 10
Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
HI, If you want both the duplicated IDs and nonduplicated IDs to be printed, ? ID <- sample(1:10, 10, replace=TRUE) ?ID # [1] 3 7 5 8 1 5 4 6 7 2 ID[!duplicated(ID)] #[1] 3 7 5 8 1 4 6 2 ?ID2<-c(4,4,4,3,4,1,2,5,7,4,3,2,5,9,8,12,"A1","A2","A1","B1") ?ID2[!duplicated(ID2)] # [1] "4"? "3"? "1"? "2"? "5"? "7"? "9"? "8"? "12" "A1" "A2" "B1" ID2<-c(4,4,4,3,4,1,2,5,7,4,3,2,5,9,8,12,12,1,3,5,8) # ID2[!duplicated(ID2)] [1]? 4? 3? 1? 2? 5? 7? 9? 8 12 I hope this is what you wanted. A.K. ----- Original Message ----- From: Weijia Wang <wwang.nyu at gmail.com> To: r-help at r-project.org Cc: Sent: Friday, August 3, 2012 3:06 AM Subject: [R] all duplicated wanted Hi, Has anyone been able to figure out how to print all duplicated observations? I have a dataset, with patients ID, and other lab records. Some patients have multiple lab records, but 'duplicated' ID will only show me the duplicates, not the original observation. How can I print both the original one and the duplicates? Thanks ______________________________________________ R-help at 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.