[R] reshape help

arun smartpink111 at yahoo.com
Mon Feb 4 14:16:26 CET 2013


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.




More information about the R-help mailing list