For each row of my data I\'d like to compute the sum of most recent value
for each group
:
dt = data.table(group = c(\'a\',\'b\',\'a\',\
using dplyr, works for many groups, but data must not be data table.
library(dplyr)
library(tidyr)
library(zoo)
dt %>%
mutate(row_number = row_number()) %>%
spread(group, value) %>%
arrange(row_number) %>%
mutate_each(funs(na.locf(., na.rm = FALSE))) %>%
mutate(answer = rowSums(.[,-1:-2], na.rm = T))
Using the above function on example data (notice data.frame()
not data.table()
:
dt = data.frame(group = c('a','b','a','a','b','a'),
value = c(10, 5, 20, 15, 15, 10),
desired = c(10, 15, 25, 20, 30, 25))
desired row_number a b answer
1 10 1 10 NA 10
2 15 2 10 5 15
3 25 3 20 5 25
4 20 4 15 5 20
5 30 5 15 15 30
6 25 6 10 15 25
dt = data.frame(group = c('a','b','c','a','a','b','c','a'),
value = c(10, 5, 20, 25, 15, 15, 30, 10),
desired = c(10, 15, 35, 50, 40, 50, 60, 55))
desired row_number a b c answer
1 10 1 10 NA NA 10
2 15 2 10 5 NA 15
3 35 3 10 5 20 35
4 50 4 25 5 20 50
5 40 5 15 5 20 40
6 50 6 15 15 20 50
7 60 7 15 15 30 60
8 55 8 10 15 30 55