[R] pivot table

arun smartpink111 at yahoo.com
Wed Nov 7 19:47:49 CET 2012


HI,

Could you tell me which version of R you are using?  I guess paste0() works from >R.2.14.
YOu can use instead:
colnames(res1)[-1]<-gsub("[\\_]",".",paste("Lab",colnames(res1)[-1],sep=""))
res1
#  ID Lab1.A Lab1.N Lab2.N Lab3.N
#1  a      1      1      1      0
#2  b      1      0      0      1
A.K.








________________________________
From: farnoosh sheikhi <farnoosh_81 at yahoo.com>
To: arun <smartpink111 at yahoo.com> 
Cc: R help <r-help at r-project.org> 
Sent: Wednesday, November 7, 2012 1:40 PM
Subject: Re: [R] pivot table


Hi there,

Thanks forhelping. I really appreciate it.
The only thing is I get this error: "Error in gsub("[\\_]", ".", paste0("Lab", colnames(res2)[-1])) : 
  could not find function "paste0""
Thanks.

Best,Farnoosh Sheikhi


________________________________
From: arun <smartpink111 at yahoo.com>
To: farnoosh sheikhi <farnoosh_81 at yahoo.com> 
Cc: R help <r-help at r-project.org> 
Sent: Tuesday, November 6, 2012 5:50 PM
Subject: Re: [R] pivot table

Hi,
May be this helps:
dat1<-read.table(text="
ID     Lab   Status
a         1          N
a       1          A
a       2          N
b       3           N
b       1          A
",sep="",header=TRUE,stringsAsFactors=FALSE)
library(reshape2)
res1<-dcast(dat1,ID~Lab+Status,value.var="Lab")
res1[,-1]<-ifelse(is.na(res1[,-1]),0,1)
colnames(res1)[-1]<-gsub("[\\_]",".",paste0("Lab",colnames(res1)[-1]))
 res1
#  ID Lab1.A Lab1.N Lab2.N Lab3.N
#1  a     
1      1      1      0
#2  b      1      0      0      1

A.K.






________________________________
From: farnoosh sheikhi <farnoosh_81 at yahoo.com>
To: arun <smartpink111 at yahoo.com> 
Cc: R help <r-help at r-project.org> 
Sent: Tuesday, November 6, 2012 7:31 PM
Subject: Re: [R] pivot table


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


________________________________
From: arun <smartpink111 at yahoo.com>
To: farnoosh sheikhi <farnoosh_81 at yahoo.com> 
Cc: R help <r-help at 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.







________________________________
From: farnoosh sheikhi <farnoosh_81 at yahoo.com>
To: arun <smartpink111 at yahoo.com> 
Cc: R help <r-help at 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


________________________________
From: arun <smartpink111 at yahoo.com>
To: farnoosh sheikhi <farnoosh_81 at yahoo.com> 
Cc: R help <r-help at 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 -----
From: farnoosh
sheikhi <farnoosh_81 at yahoo.com>
To: "r-help at R-project.org" <r-help at 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 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