[R] Combine subsets by factor level

arun smartpink111 at yahoo.com
Wed Jun 6 21:24:13 CEST 2012


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