r - Adding a column to a ragged array matrix by grouping variables -
in r:
i not sure proper title question is, maybe can me out. appreciated. i'm sorry if called searchable.
so have ragged array matrix (multiple upcs)
[upc] [quantity] [day] [daysum] [1] 123 11 1 na [2] 123 2 1 na [3] 789 5 1 na [4] 456 10 2 na [5] 789 6 2 na
i want matrix summed upc each day, example:
[upc] [quantity] [day] [daysum] [1] 123 11 1 13 [2] 123 2 1 13 [3] 789 5 1 5 [4] 456 10 2 10 [5] 789 6 2 6
thank time , help.
you have not described supposed happen "clean matrix" code create "column" larger matrix suitable binding on row-aligned basis quite simple:
b <- cbind(b, daysum=ave(b[, 'quantity'], # analysis variable b[, 'upc'], b[ , 'day'], # strata variables fun=sum) ) # function applied in strata
this of course assumes b has column names indicated. should work if dataframe, although output not suggest have r objects yet. ave
function replicate sum
s rows same stratification variables.
Comments
Post a Comment