Hi again,

I have another question for different data.

Here is how my data looks like:
ID     Lab   Status
a         1          N
a   1          A
a   2          N
b   3       N
b   1          A
 
I need to reformat this data as following.

ID   Lab1.N   Lab1.A   Lab2.N    Lab2.A    Lab3.N    Lab3.A
a           11       1000
b    0    1001               0



Thanks a lot:)

Best,Farnoosh Sheikhi


________________________________


Cc: R help <r-help@r-project.org> 
Sent: Tuesday, November 6, 2012 2:44 PM
Subject: Re: [R] pivot table

Hi,

Is this okay?
library(reshape2)
dat1<-read.table(text="
ID   Diag   Proc  DOB  Gender   
a    diag1    proc1   10/15/1969 M  
b    diag2    proc2    8/25/1978 F
c    diag1    proc1    1/10/1985 M 
a    diag3    proc3    10/15/1969 M
b    diag4    proc4    8/25/1978 F
b    diag6    proc5    8/25/1978 F
",sep="",header=TRUE,stringsAsFactors=FALSE)
res<-dcast(melt(dat1,id.var=c("ID","DOB","Gender")),ID+Gender+DOB~value)
res[,4:13]<-ifelse(is.na(res[,4:13]),0,1)
res
#  ID Gender        DOB diag1 diag2 diag3 diag4 diag6 proc1 proc2 proc3 proc4
#1  a      M 10/15/1969     1     0     1     0     0     1     0     1     0
#2  b      F  8/25/1978     0     1     0     1     1     0     1     0     1
#3  c      M  1/10/1985     1     0     0     0     0     1     0     0     0
 # proc5
#1     0
#2     1
#3     0

A.K.







________________________________


Cc: R help <r-help@r-project.org> 
Sent: Tuesday, November 6, 2012 5:12 PM
Subject: Re: [R] pivot table


Hi there,

Thanks a lot for your help, but Proc doesn't show in the result.
I also want to assign 1 and 0 instead of the name of variables.
Thanks a lot.

Best,Farnoosh Sheikhi


________________________________


Cc: R help <r-help@r-project.org> 
Sent: Tuesday, November 6, 2012 1:42 PM
Subject: Re: [R] pivot table

HI,

It is better to dput() an example dataset to work with.

May be this helps:

dat1<-read.table(text="
ID   Diag   Proc  DOB  Gender   
a    diag1    1   10/15/1969 M  
b    diag2    2    8/25/1978 F
c    diag1    1    1/10/1985 M 
a    diag3    3    10/15/1969 M
b    diag4    4    8/25/1978 F
b    diag4    5    8/25/1978 F
",sep="",header=TRUE,stringsAsFactors=FALSE)
library(reshape)
res<-reshape(dat1,idvar=c("ID","DOB","Gender"),v.names="Diag",timevar="Proc",direction="wide")
 res
#  ID        DOB Gender Diag.1 Diag.2 Diag.3 Diag.4
Diag.5
#1  a 10/15/1969      M  diag1   <NA>  diag3   <NA>   <NA>
#2  b  8/25/1978      F   <NA>  diag2   <NA>  diag4  diag4
#3  c  1/10/1985      M  diag1   <NA>   <NA>   <NA>   <NA>


A.K.




----- Original Message -----

To: "r-help@R-project.org" <r-help@r-project.org>
Cc: 
Sent: Tuesday, November 6, 2012 3:37 PM
Subject: [R] pivot
table

Hello,

I have a data which looks like below: Some of the patients have multiple diagnosis.

ID(200 patients)   Diag (100 unique Diag-200 in general)   Proc (50 uniqe Proc)  DOB (200)   Gender (200)   
a                           daig1
b                           diag2
c                            diag1

I want to reformat this data to :

ID   diag1 diag 2 diag 3..  diagx   proc1   proc2   proc3... procx  DOB  Gender
a1011  20F

Thanks a lot for your help and time.

Best,Farnoosh Sheikhi
    [[alternative HTML version deleted]]


______________________________________________
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.   
	[[alternative HTML version deleted]]

