Relative Content

Tag Archive for rdplyrzoo

Return NA for conditional lead/lagged rolling sum if window outside dataframe or group

I need to get the sum of values from column x either side of every 1 in column y. The windows are 1:4 and 5:8 before and after each occurrence of 1 in y. However, if the windows are beyond the limits of the df, 0 is returned. This is an issue as it is possible that 0 could be a valid result. The full data have multiple groups (id) but hopefully this repex is enough help create a scalable solution.

Return NA for conditional lead/lagged rolling sum if window outside dataframe or group

I need to get the sum of values from column x either side of every 1 in column y. The windows are 1:4 and 5:8 before and after each occurrence of 1 in y. However, if the windows are beyond the limits of the df, 0 is returned. This is an issue as it is possible that 0 could be a valid result. The full data have multiple groups (id) but hopefully this repex is enough help create a scalable solution.