[R] Combine subsets by factor level

arun smartpink111 at yahoo.com
Wed Jun 6 23:26:46 CEST 2012


Hi,

So it might be in this format.  Same code works.


 dat6<-data.frame(patient=c(c(1:3)[rep(c(1,1,1,2,2,3,3,3,3))],rep(c(4:10),rep(3,7))), var=c("cycle0","cycle1","cycle2","cycle5","cycle12")[rep(c(1,2,3,1,5,1,2,3,4,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3))],value=c(rnorm(30,1,0.5)))


> dat6
   patient     var      value
1        1  cycle0 1.56422778
2        1  cycle1 0.58855364
3        1  cycle2 0.80263892
4        2  cycle0 1.12141258
5        2 cycle12 1.59177846
6        3  cycle0 0.09706219
7        3  cycle1 0.62707806
8        3  cycle2 1.67569203
9        3  cycle5 1.46231543
10       4  cycle0 0.22468741
11       4  cycle1 1.78807770
12       4  cycle2 2.10534150
13       5  cycle0 0.71970367
14       5  cycle1 0.80972468
15       5  cycle2 0.37859704
16       6  cycle0 1.72754736
17       6  cycle1 0.24323640
18       6  cycle2 0.67442840
19       7  cycle0 0.73625734
20       7  cycle1 0.74946198
21       7  cycle2 0.05603698
22       8  cycle0 0.87371378
23       8  cycle1 1.21543691
24       8  cycle2 0.87648443
25       9  cycle0 1.28035157
26       9  cycle1 0.99059664
27       9  cycle2 1.01691054
28      10  cycle0 1.02381366
29      10  cycle1 0.64904309
30      10  cycle2 2.28296855
> dat7<-cast(dat6,patient~var,value="value")
> dat7
   patient     cycle0    cycle1  cycle12     cycle2   cycle5
1        1 1.56422778 0.5885536       NA 0.80263892       NA
2        2 1.12141258        NA 1.591778         NA       NA
3        3 0.09706219 0.6270781       NA 1.67569203 1.462315
4        4 0.22468741 1.7880777       NA 2.10534150       NA
5        5 0.71970367 0.8097247       NA 0.37859704       NA
6        6 1.72754736 0.2432364       NA 0.67442840       NA
7        7 0.73625734 0.7494620       NA 0.05603698       NA
8        8 0.87371378 1.2154369       NA 0.87648443       NA
9        9 1.28035157 0.9905966       NA 1.01691054       NA
10      10 1.02381366 0.6490431       NA 2.28296855       NA

A.K.


________________________________
From: Lib Gray <libgray3827 at gmail.com>
To: arun <smartpink111 at yahoo.com> 
Cc: R help <r-help at r-project.org> 
Sent: Wednesday, June 6, 2012 4:52 PM
Subject: Re: [R] Combine subsets by factor level


They do not; each patient only has rows for the cycles it has. Some have only one, some have more than ten. 
On Jun 6, 2012 2:24 PM, "arun" <smartpink111 at yahoo.com> wrote:

Hi,
>
>Try this:
>
>library(reshape)
>
>dat4<-data.frame(patient=rep(c(1:10),
>rep(3,10)),var=rep(c("cycle0","cycle1","cycle2"),rep(1,3)),value=c(rnorm(15,1,0.5),NA,rnorm(5,1,0.5),NA,rnorm(8,1,0.5)))
>> dat5<-cast(dat4,patient~var,value="value")
>> dat5
>   patient    cycle0    cycle1    cycle2
>1        1 1.8826827 1.0316985 1.0084754
>2        2 1.1822553 1.5494087 0.9173749
>3        3 0.3935503 0.7012282 0.5213031
>4        4 0.8330390 0.6430550 0.7751283
>5        5 1.4092714 0.8120330 0.6255491
>6        6        NA 0.1068520 0.7556006
>7        7 1.4322698 1.6109262 0.9650534
>8        8        NA 0.3861208 1.1349206
>9        9 1.5659958 1.8725942 1.5676570
>10      10 1.0895054 1.1941775 1.3932515
>
>
>For the missing values, I assume that cycle will be in the dataset on the longformat and its value as NA. 
>
>
>A.K.
>
>
>________________________________
>From: Lib Gray <libgray3827 at gmail.com>
>To: arun <smartpink111 at yahoo.com>
>Cc: R help <r-help at r-project.org>
>Sent: Wednesday, June 6, 2012 2:28 PM
>Subject: Re: [R] Combine subsets by factor level
>
>
>Yes, except that patients have different cycle numbers. Such as, one might have cycle 1,2,3, and another has 1,4,12.
>On Jun 6, 2012 12:54 PM, "arun" <smartpink111 at yahoo.com> wrote:
>
>Hi Iglucia,
>>
>>I am not sure how your dataset looks like.  Does it look similar to this:
>>
>>> dat4<-data.frame(patient=rep(c(1:10), rep(3,10)),var=rep(c("cycle0","cycle1","cycle2"),rep(1,3)),value=c(rnorm(15,1,0.5),NA,rnorm(5,1,0.5),NA,rnorm(8,1,0.5)))
>>> dat4
>>   patient    var     value
>>1        1 cycle0 1.8826827
>>2        1 cycle1 1.0316985
>>3        1 cycle2 1.0084754
>>4        2 cycle0 1.1822553
>>5        2 cycle1 1.5494087
>>6        2 cycle2 0.9173749
>>7        3 cycle0 0.3935503
>>8        3 cycle1 0.7012282
>>9        3 cycle2 0.5213031
>>10       4 cycle0 0.8330390
>>11       4 cycle1 0.6430550
>>12       4 cycle2 0.7751283
>>13       5 cycle0 1.4092714
>>14       5 cycle1 0.8120330
>>15       5 cycle2 0.6255491
>>16       6 cycle0        NA
>>17       6 cycle1 0.1068520
>>18       6 cycle2 0.7556006
>>19       7 cycle0 1.4322698
>>20       7 cycle1 1.6109262
>>21       7 cycle2 0.9650534
>>22       8 cycle0        NA
>>23       8 cycle1 0.3861208
>>24       8 cycle2 1.1349206
>>25       9 cycle0 1.5659958
>>26       9 cycle1 1.8725942
>>27       9 cycle2 1.5676570
>>28      10 cycle0 1.0895054
>>29      10 cycle1 1.1941775
>>30      10 cycle2 1.3932515
>>
>>
>>
>> 
>>
>>A.K.
>>
>>
>>
>>
>>
>>
>>----- Original Message -----
>>From: lglucia <libgray3827 at gmail.com>
>>To: r-help at r-project.org
>>Cc:
>>Sent: Wednesday, June 6, 2012 12:07 AM
>>Subject: [R] Combine subsets by factor level
>>
>>I'm attempting to change a data set by compressing rows into columns.
>>Currently there are several rows that all have information about one
>>"patient," but at different cycles. I'm trying to make each patient only
>>have one row in the data set.
>>
>>Does anyone know a good way to combine data sets by factor level? I've
>>separated the groups into different subsets by cycle, but not every patient
>>has data for every cycle (i.e. there are 1200 who have cycle 0, but only 200
>>of those have a cycle 1, and a different number have cycles higher than
>>that, etc). I then made the patient number the identifying label. If there
>>is a way to column-combine these subsets by the factor level of these
>>patient names, and leave any patients that are missing a cycle as NA?
>>
>>If anyone has insight on how to do this, or a better way to complete what
>>I'm trying to do, I'd appreciate it!
>>
>>--
>>View this message in context: http://r.789695.n4.nabble.com/Combine-subsets-by-factor-level-tp4632472.html
>>Sent from the R help mailing list archive at Nabble.com.
>>
>>______________________________________________
>>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