[R] reshape

Henrique Dallazuanna wwwhsd at gmail.com
Sun Feb 10 23:20:05 CET 2008


reshape(dat, direction="wide", timevar="tr", idvar=c("id", "code","sp" ))[,2:6]

But, I don't understand why you use reshape

On 10/02/2008, juli pausas <pausas at gmail.com> wrote:
> Dear colleagues,
> I'd like to reshape a datafame in a long format to a wide format, but
> I do not quite get what I want. Here is an example of the data I've
> have (dat):
>
> sp <- c("a", "a", "a", "a", "b", "b", "b", "c", "d", "d", "d", "d")
> tr <- c("A", "B", "B", "C", "A", "B", "C", "A", "A", "B", "C", "C")
> code <- c("a1", "a2", "a2", "a3", "a3", "a3", "a4", "a4", "a4", "a5",
> "a5", "a6")
> dat <- data.frame(id=1:12, sp=sp, tr=tr, val=31:42, code=code)
>
> and below is what I'd like to obtain. That is, I'd like the tr
> variable in different columns (as a timevar) with their value (val).
>
> sp  code  tr.A  tr.B  tr.C
> a    a1   31    NA    NA
> a    a2   NA    32    NA
> a    a2   NA    33    NA    **
> a    a3   NA    NA    34
> b    a3   35    36    NA
> b    a4   NA    NA    37
> c    a4   38    NA    NA
> d    a4   39    NA    NA
> d    a5   NA    40    41
> d    a6   NA    NA    42
>
> Using reshape:
>
> reshape(dat[,2:5], direction="wide", timevar="tr", idvar=c("code","sp" ))
>
> I'm getting very close. The only difference is in the 3rd row (**),
> that is when sp and code are the same I only get one record. Is there
> a way to get all records? Any idea?
>
> Thank you very much for any help
>
> Juli Pausas
>
> --
> http://www.ceam.es/pausas
>
> ______________________________________________
> 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.
>


-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list