Dear R users - I have a list of patient identifiers and diagnoses from inpatient admissions. I would like to reorganize the list, presently in a long format to a wide format in reshape, but in the absence of a "time" element, I am uncertain how to do this - any help greatly appreciated. ID Dx A nausea A diabetes A kidney failure A heart attack A fever B fever B pneumonia B heart attack B nausea B cough C kidney failure C nausea C foot pain I want this to be ID nausea diabetes kidney_failure heart_attack fever pneumonia A 1 1 1 1 1 0 B 0 0 0 1 1 1 R. Matthew Strother, MD Clinical Pharmacology Medical Oncology [[alternative HTML version deleted]]
Hi Robert, how about this? tmp<-read.table(text="ID Dx A nausea A diabetes A kidney_failure A heart_attack A fever B fever B pneumonia B heart_attack B nausea B cough C kidney_failure C nausea C foot_pain",header=T) data.frame(cbind(ID=levels(tmp$ID),(table(tmp$ID,tmp$Dx)))) hth. Am 04.02.2013 09:16, schrieb Robert Strother:> Dear R users - > > I have a list of patient identifiers and diagnoses from inpatient > admissions. I would like to reorganize the list, presently in a long > format to a wide format in reshape, but in the absence of a "time" element, > I am uncertain how to do this - any help greatly appreciated. > > ID Dx > A nausea > A diabetes > A kidney failure > A heart attack > A fever > B fever > B pneumonia > B heart attack > B nausea > B cough > C kidney failure > C nausea > C foot pain > > I want this to be > ID nausea diabetes kidney_failure heart_attack fever pneumonia > A 1 1 1 1 1 > 0 > B 0 0 0 1 1 > 1 > > > R. Matthew Strother, MD > Clinical Pharmacology > Medical Oncology > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790 -- Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG): Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg Vorstandsmitglieder: Prof. Dr. Martin Zeitz (Vorsitzender), Dr. Alexander Kirstein, Joachim Pr?l?, Prof. Dr. Dr. Uwe Koch-Gromus
Hi, Try this: library(reshape2) dat1<-read.table(text="ID ?Dx A ? ?nausea A ? ?diabetes A ? ?kidney_failure A ? ?heart_attack A ? ?fever B ? ?fever B ? ?pneumonia B ? ?heart_attack B ? ?nausea B ? ?cough C ? ?kidney_failure C ? ?nausea C ? ?foot_pain",header=T,stringsAsFactors=F,sep="") ?dcast(dat1,ID~Dx,length,value.var="Dx") ?# ID cough diabetes fever foot_pain heart_attack kidney_failure nausea #1 ?A ? ? 0 ? ? ? ?1 ? ? 1 ? ? ? ? 0 ? ? ? ? ? ?1 ? ? ? ? ? ? ?1 ? ? ?1 #2 ?B ? ? 1 ? ? ? ?0 ? ? 1 ? ? ? ? 0 ? ? ? ? ? ?1 ? ? ? ? ? ? ?0 ? ? ?1 #3 ?C ? ? 0 ? ? ? ?0 ? ? 0 ? ? ? ? 1 ? ? ? ? ? ?0 ? ? ? ? ? ? ?1 ? ? ?1 ? #pneumonia #1 ? ? ? ? 0 #2 ? ? ? ? 1 #3 ? ? ? ? 0 A.K. ----- Original Message ----- From: Robert Strother <rstrothe at gmail.com> To: r-help at r-project.org Cc: Sent: Monday, February 4, 2013 3:16 AM Subject: [R] reshape help Dear R users - I have a list of patient identifiers and diagnoses from inpatient admissions.? I would like to reorganize the list, presently in a long format to a wide format in reshape, but in the absence of a "time" element, I am uncertain how to do this - any help greatly appreciated. ID? Dx A? ? nausea A? ? diabetes A? ? kidney failure A? ? heart attack A? ? fever B? ? fever B? ? pneumonia B? ? heart attack B? ? nausea B? ? cough C? ? kidney failure C? ? nausea C? ? foot pain I want this to be ID? ? nausea? diabetes? kidney_failure? heart_attack? fever? pneumonia A? ? ? ? ? 1? ? ? ? ? ? 1? ? ? ? ? ? 1? ? ? ? ? ? ? ? ? 1? ? ? ? ? ? ? 1 ? ? ? ? 0 B? ? ? ? ? 0? ? ? ? ? ? 0? ? ? ? ? ? 0? ? ? ? ? ? ? ? ? 1? ? ? ? ? ? ? 1 ? ? ? ? 1 R. Matthew Strother, MD Clinical Pharmacology Medical Oncology ??? [[alternative HTML version deleted]] ______________________________________________ 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.