[R] Updating a list.

rkevinburton at charter.net rkevinburton at charter.net
Thu Aug 28 01:18:56 CEST 2008


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

Where the Quantity column is the sum of the Quantity column in the previous list with the same 'Sku'. Notice there are no entries for 1,2 and 4 so those columns are zero and in the small example there are only two columns for DayOfYear 3, so the sum is 2. If it helps I have included hints to the data structure that I am working with below.

The parts that are particularly hard for me is how I would simultaneously remove and replace a list and conditionally sum only if a value exists.

Thank you.

Kevin

dput(r007[1])

structure(list("100026" = structure(list(DayOfYear = c(3L, 30L, 
149L, 156L, 165L, 171L, 187L, 198L, 212L, 215L, 216L, 218L, 221L, 
224L, 226L, 227L), Quantity = c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("DayOfYear", "Quantity"
), row.names = c(NA, 16L), class = c("cast_df", "data.frame"), idvars = "DayOfYear", rdimnames = list(
    structure(list(DayOfYear = c(3L, 30L, 149L, 156L, 165L, 171L, 
    187L, 198L, 212L, 215L, 216L, 218L, 221L, 224L, 226L, 227L
    )), .Names = "DayOfYear", row.names = c("3", "30", "149", 
    "156", "165", "171", "187", "198", "212", "215", "216", "218", 
    "221", "224", "226", "227"), class = "data.frame"), structure(list(
        variable = structure(1L, .Label = "Quantity", class = "factor")), .Names = "variable", row.names = "Quantity", class = "data.frame")))), .Names = "100026")

> str(r2007[1])
List of 1
 $ 100009:List of 2
 $ DayOfYear: int [1:4] 66 128 137 193
 $ Quantity : int [1:4] 1 1 1 1
 - attr(*, "row.names")= int [1:4] 1 2 3 4
 - attr(*, "idvars")= chr "DayOfYear"
 - attr(*, "rdimnames")=List of 2
  ..$ :'data.frame':    4 obs. of  1 variable:
  .. ..$ DayOfYear: int [1:4] 66 128 137 193
  ..$ :'data.frame':    1 obs. of  1 variable:
  .. ..$ variable: Factor w/ 1 level "Quantity": 1



More information about the R-help mailing list