Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting a data frame to a matrix with plyr daply
    primarykey
    data
    text
    <p>I'm trying to use the <code>daply</code> function in the <code>plyr</code> 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:</p> <pre><code> 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 </code></pre> <p>And I would like to be able to visualize the data in a matrix format, which would look something like this:</p> <pre><code> Month Vehicle Oct-10 Nov-10 Dec-10 31057 256 267 159 31059 316 293 268 31060 348 250 206 </code></pre> <p>Here are a couple of alternative syntax that I use (the latter because my original dataframe has more columns than I show here):</p> <pre><code>daply(DF, .(Vehicle, Month), identity) daply(DF,.(Vehicle,Month), colwise(identity,.(Samples))) </code></pre> <p>However what I get instead is rather abstruse:</p> <pre><code> 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 </code></pre> <p>I used the <code>str</code> function on the output as some commenters have suggested, and here is an excerpt:</p> <pre><code>List of 9 $ :'data.frame': 1 obs. of 3 variables: ..$ Month : Ord.factor w/ 3 levels "Oct-10"&lt;"Nov-10"&lt;..: 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"&lt;"Nov-10"&lt;..: 1 ..$ Vehicle: Factor w/ 3 levels "31057","31059",..: 2 ..$ Samples: int 316 </code></pre> <p>What am I missing? Also, is there a way to do this simply with the base packages? Thanks!</p> <p>Below is the <code>Dput</code> of the data frame if you'd like to reproduce this:</p> <pre><code>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)) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload