[R] Updating a list.

hadley wickham h.wickham at gmail.com
Thu Aug 28 02:43:02 CEST 2008


On Wed, Aug 27, 2008 at 6:18 PM,  <rkevinburton at charter.net> wrote:
> I have a list that is generated from the resape package function 'cast'. It consists of three columns, Sku, DayOfYear, variable it is generated like:
>
> r2007 <- cast(m2008, DayOfYear ~ variable | Sku, sum)
>
> Now DayOfYear can range from 1:365 but there are not necessarily that many rows in the list. What I want to do is make every row in the list of lenght 365 and have the values correspond to the sum of the DayOfYear in the list if present and 0 if not. For example the first item in the list looks like:
>
> $`100026`
>   DayOfYear Quantity
> 1          3        1
> 2         30        1
> 3        149        1
> 4        156        1
> 5          3         1
>
> Notice that there are not entires for each 'DayOfYear'. I would like to replace list list maintaining the same data structure with something like:
>
> $`100026`
>   DayOfYear Quantity
> 1          1        0
> 2          2        0
> 3          3        2
> 4          4        0
>

Try this:

m2008$DayOfYear <- factor(m2008$DayOfYear, levels = 1:365)
r2007 <- cast(m2008, DayOfYear ~ variable | Sku, sum, fill = 0)

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list