[R] reshape

arun smartpink111 at yahoo.com
Sun Nov 17 16:38:11 CET 2013


Felipe,

I get the results like this by running the code:
z <-read.table(text="date week length
7/13/2010 28 34
7/13/2010 28 35
7/14/2010 28 35
7/14/2010 28 35
7/14/2010 28 36
7/14/2010 28 36
7/20/2010 29 31
7/16/2010 29 34
7/18/2010 29 34
7/18/2010 29 34
7/21/2010 29 35
7/20/2010 29 36
7/21/2010 29 36
7/22/2010 29 36
7/16/2010 29 37
7/18/2010 29 37
7/20/2010 29 37
7/21/2010 29 37
7/21/2010 29 37
7/22/2010 29 37
7/22/2010 29 37",header=TRUE)

z$ID <- with(z,ave(seq_along(date),date,week,FUN=seq_along))

library(reshape2)
dcast(z,date+ID~week,value.var="length")[,-2]
        date 28 29
1  7/13/2010 34 NA
2  7/13/2010 35 NA
3  7/14/2010 35 NA
4  7/14/2010 35 NA
5  7/14/2010 36 NA
6  7/14/2010 36 NA
7  7/16/2010 NA 34
8  7/16/2010 NA 37
9  7/18/2010 NA 34
10 7/18/2010 NA 34
11 7/18/2010 NA 37
12 7/20/2010 NA 31
13 7/20/2010 NA 36
14 7/20/2010 NA 37
15 7/21/2010 NA 35
16 7/21/2010 NA 36
17 7/21/2010 NA 37
18 7/21/2010 NA 37
19 7/22/2010 NA 36
20 7/22/2010 NA 37
21 7/22/2010 NA 37

##Also, didn't got any error with ?reshape()
If you don't want the `dates`, then:
 z$ID <- with(z,ave(seq_along(week),week,FUN=seq_along))
dcast(z,ID~week,value.var="length")[,-1]
   28 29
1  34 31
2  35 34
3  35 34
4  35 34
5  36 35
6  36 36
7  NA 36
8  NA 36
9  NA 37
10 NA 37
11 NA 37
12 NA 37
13 NA 37
14 NA 37
15 NA 37

A.K.
 



On Sunday, November 17, 2013 3:57 AM, Felipe Carrillo <mazatlanmexico at yahoo.com> wrote:

Thanks Arun,
I
 tried and got an error message, also the first line of code is showing 
the date as column header but I only want the week as column header. You
 can get rid of the date, I only need the week as column header and the 
values below it.
 dcast(z,date+ID~week,value.var="length")[,-2]
  date 7/13/2010 7/14/2010 7/16/2010 7/18/2010 7/20/2010 7/21/2010 7/22/2010
1 2010        28        28        29        29        29        29        29
2
2010        28        28        29        29        29        29        29
3 2010        NA        28        NA        29        29        29        29
4 2010        NA        28        NA        NA        NA        29       
NA
> reshape(z,direction="wide",idvar=c("date","ID"),timevar="week")[,-2]
Error in `[.data.frame`(thistime, match(rval[, idvar], thistime[, idvar]),  : 
  undefined columns selected


My dataset based on the "z" dataset should look like this:
28     29  <<<<<Column headers
35     31
35     34
35     34
36     35
36     36
         36
         36
         37
         37
         37
         37
         37
         37
         37


Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA
http://www.fws.gov/redbluff/rbdd_jsmp.aspx




On Sunday, November 17, 2013 12:27 AM, arun <smartpink111 at yahoo.com> wrote:

HI Felipe,
>May be this works for you:
>z$ID <- with(z,ave(seq_along(date),date,week,FUN=seq_along))
>
>library(reshape2)
>dcast(z,date+ID~week,value.var="length")[,-2]
>
>#or
>reshape(z,direction="wide",idvar=c("date","ID"),timevar="week")[,-2]
>
>
>A.K.
>
>
>
>
>
>
>
>
>
>
>On Sunday, November 17, 2013 1:24 AM, Felipe Carrillo <mazatlanmexico at yahoo.com> wrote:
>
>
>
>Hello Arun, I was wondering if you can help me with this question
>
> How can I put the values of week(28,29) as column headers
>and put the corresponding length values on each week number?
>I heard about dcast but don't know how to use it yet.
>z <-read.table(text="date week length
>7/13/2010   28     34
>7/13/2010   28     35
>7/14/2010  
28     35
>7/14/2010   28     35
>7/14/2010   28     36
>7/14/2010   28     36
>7/20/2010   29     31
>7/16/2010   29     34
>7/18/2010   29     34
>7/18/2010   29     34
>7/21/2010   29     35
>7/20/2010   29     36
>7/21/2010   29     36
>7/22/2010   29     36
>7/16/2010   29     37
>7/18/2010   29     37
>7/20/2010  
29    
>37
>7/21/2010   29     37
>7/21/2010   29     37
>7/22/2010   29     37
>7/22/2010   29     37",header=TRUE)
>
>
>
>
>
>
>
>
>
>
>
>Felipe D. Carrillo
>Supervisory Fishery Biologist
>Department of the Interior
>US Fish & Wildlife Service
>California, USA
>http://www.fws.gov/redbluff/rbdd_jsmp.aspx
>
>



More information about the R-help mailing list