[R] Reshape2 sytax

Dennis Murphy djmuser at gmail.com
Tue Aug 9 00:12:14 CEST 2011


Hi:

Here are two ways you could do this, one using dcast() and one using xtabs():

batdat <- structure(list(Time = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L), .Label = c("0:00", "0:15", "0:30", "0:45", "1:00", "1:15"
), class = "factor"), Label = structure(c(1L, 2L, 4L, 4L, 1L,
1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L), .Label = c("Morbla", "Ptemac", "Ptepar", "Ptequa"
), class = "factor"), Number = c(2L, 27L, 21L, 1L, 3L, 1L, 18L,
49L, 2L, 7L, 0L, 18L, 2L, 26L, 1L, 12L, 4L, 18L, 0L, 20L, 4L,
5L, 1L, 16L)), .Names = c("Time", "Label", "Number"), class =
"data.frame", row.names = c(NA,
-24L))

library('reshape2')

> dcast(batdat, Time ~ Label, fun.aggregate = sum, value_var = 'Number')
  Time Morbla Ptemac Ptepar Ptequa
1 0:00      2     27      0     22
2 0:15      4     18      0     49
3 0:30      2      7      0     18
4 0:45      2     26      1     12
5 1:00      4     18      0     20
6 1:15      4      5      1     16

> with(batdat, xtabs(Number ~ Time + Label))
      Label
Time   Morbla Ptemac Ptepar Ptequa
  0:00      2     27      0     22
  0:15      4     18      0     49
  0:30      2      7      0     18
  0:45      2     26      1     12
  1:00      4     18      0     20
  1:15      4      5      1     16

You need an aggregation function in dcast() because you have at least
two cases where the same variable is measured twice at the same time.
If that's not supposed to happen, check against this:

> cast(batdat, Time ~ Label, value = 'Number')
Aggregation requires fun.aggregate: length used as default
  Time Morbla Ptemac Ptepar Ptequa
1 0:00      1      1      0      2             # <---
2 0:15      2      1      0      1             # <---
3 0:30      1      1      1      1
4 0:45      1      1      1      1
5 1:00      1      1      1      1
6 1:15      1      1      1      1

HTH,
Dennis

On Mon, Aug 8, 2011 at 11:48 AM, Bruce Miller <batsncats at gmail.com> wrote:
> Hi Hadley et all,
>
> I am struggling with reshape2 and melt works and melt_check
> (filtered151) seems fine
>
> My cast command was acast (filtered151, Time ~ Species ~ Number)
>
>  > melt_check (filtered151)
> Using time, Species as id variables
> $id
> [1] "time"    "Species"
>
> $measure
> [1] "Number"
>
> When I execute cast the data matrix is in the correct order but the data
> values are not included.
> Clearly I am missing something.  I expected / need to have the column
> values to  = the values from the input data set for each time block.
>
>     Morbla Ptemac Ptepar Ptequa
> 0:00       0      0      0      0
> 0:15       0      0      0      0
> 0:30       0      0      0      0
> 0:45       0      0      0      0
> 1:00       0      0      0      0
> 1:15       0      0      0      0
> 1:30       0      0      0      0
> 1:45       0      0      0      0
> 18:45      0      0      0      0
> 19:00      0      0      0      0
> 19:15      0      0      0      0
> 19:30      0      0      0      0
> 19:45      0      0      0      0
> 2:00       0      0      0      0
> 2:15       0      0      0      0
> 2:30       0      0      0      0
> 2:45       0      0      0      0
> 20:00      0      0      0      1
> 20:15      0      0      0      0
> 20:30      0      0      0      0
> 20:45      0      0      0      0
> 21:00      0      0      0      0
> 21:15      0      0      0      0
> 21:30      0      0      0      0
> 21:45      0      0      0      0
> 22:00      0      0      0      0
> 22:15      0      0      0      0
> 22:30      0      0      0      0
> 22:45      0      0      0      0
> 23:00      0      0      0      0
> 23:15      0      0      0      0
> 23:30      0      0      0      0
> 23:45      0      0      0      0
> 3:00       0      0      0      0
> 3:15       0      0      0      0
> 3:30       0      0      0      0
> 3:45       0      0      0      0
> 4:00       0      0      0      0
> 4:15       0      0      0      0
> 4:30       0      0      0      0
> 4:45       0      0      0      0
> 5:00       0      0      0      0
> 5:15       0      0      0      0
> 5:30       0      0      0      0
> 5:45       0      0      0      0
> 6:00       0      0      0      0
> 6:15       0      0      0      0
> 6:30       0      0      0      0
> 6:45       0      0      0      0
> 7:00       0      0      0      0
>
>
> Data is in long format like this:
>
> Time    Label   Number
> 0:00    Morbla  2
> 0:00    Ptemac  27
> 0:00    Ptequa  21
> 0:00    Ptequa  1
> 0:15    Morbla  3
> 0:15    Morbla  1
> 0:15    Ptemac  18
> 0:15    Ptequa  49
> 0:30    Morbla  2
> 0:30    Ptemac  7
> 0:30    Ptepar  0
> 0:30    Ptequa  18
> 0:45    Morbla  2
> 0:45    Ptemac  26
> 0:45    Ptepar  1
> 0:45    Ptequa  12
> 1:00    Morbla  4
> 1:00    Ptemac  18
> 1:00    Ptepar  0
> 1:00    Ptequa  20
> 1:15    Morbla  4
> 1:15    Ptemac  5
> 1:15    Ptepar  1
> 1:15    Ptequa  16
>
> ...etc
>
> Bruce
>
> --
> Bruce W. Miller, Ph.D.
> Conservation Ecologist
> Neotropical Bat Project
>
>
> office details
> Gallon Jug, Belize
> Mailing address
> P.O. Box 37, Belize City
> Belize, Central America
> Phone +501-220-9002
>
>
>
>        [[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