I'm trying to use the daply
function in the plyr
package but I cannot get it to output properly. Even though the variable that makes up the matrix is numeric, the elements of the matrix are lists, not the variable itself. Here is a small subset of the data for example sake:
Month Vehicle Samples
1 Oct-10 31057 256
2 Oct-10 31059 316
3 Oct-10 31060 348
4 Nov-10 31057 267
5 Nov-10 31059 293
6 Nov-10 31060 250
7 Dec-10 31057 159
8 Dec-10 31059 268
9 Dec-10 31060 206
And I would like to be able to visualize the data in a matrix format, which would look something like this:
Month
Vehicle Oct-10 Nov-10 Dec-10
31057 256 267 159
31059 316 293 268
31060 348 250 206
Here are a couple of alternative syntax that I use (the latter because my original dataframe has more columns than I show here):
daply(DF, .(Vehicle, Month), identity)
daply(DF,.(Vehicle,Month), colwise(identity,.(Samples)))
However what I get instead is rather abstruse:
Month
Vehicle Oct-10 Nov-10 Dec-10
31057 List,3 List,3 List,3
31059 List,3 List,3 List,3
31060 List,3 List,3 List,3
I used the str
function on the output as some commenters have suggested, and here is an excerpt:
List of 9
$ :'data.frame': 1 obs. of 3 variables:
..$ Month : Ord.factor w/ 3 levels "Oct-10"<"Nov-10"<..: 1
..$ Vehicle: Factor w/ 3 levels "31057","31059",..: 1
..$ Samples: int 256
$ :'data.frame': 1 obs. of 3 variables:
..$ Month : Ord.factor w/ 3 levels "Oct-10"<"Nov-10"<..: 1
..$ Vehicle: Factor w/ 3 levels "31057","31059",..: 2
..$ Samples: int 316
What am I missing? Also, is there a way to do this simply with the base packages? Thanks!
Below is the Dput
of the data frame if you'd like to reproduce this:
structure(list(Month = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L), .Label = c("Oct-10", "Nov-10", "Dec-10"), class = c("ordered",
"factor")), Vehicle = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L), .Label = c("31057", "31059", "31060"), class = "factor"),
Samples = c(256L, 316L, 348L, 267L, 293L, 250L, 159L, 268L,
206L)), .Names = c("Month", "Vehicle", "Samples"), class = "data.frame", row.names = c(NA,
9L))