Re: [R] Take average of previous weeks

2018-03-26 Thread Bert Gunter
The row means **are** new variables and can be put wherever you like. But all columns in a data frame **must** have the same number of rows, so you'll have to fill in missing values as appropriate. That's where you need to "adjust as necessary to your needs." -- Bert Bert Gunter "The trouble

Re: [R] Take average of previous weeks

2018-03-26 Thread Miluji Sb
Dear Bert, Thank you very much.This works. I was wondering if the fact that I want to create new variables (sorry for not stating that fact) makes any difference? Thank you again. Sincerely, Milu On Sun, Mar 25, 2018 at 10:05 PM, Bert Gunter wrote: > I am sure that

Re: [R] Take average of previous weeks

2018-03-25 Thread Bert Gunter
I am sure that this sort of thing has been asked and answered before, so in case my suggestions don't work for you, just search the archives a bit more. I am also sure that it can be handled directly by numerous functions in numerous packages, e.g. via time series methods or by calculating running

Re: [R] Take average of previous weeks

2018-03-25 Thread Gabor Grothendieck
There is no `value` column in the `dput` output shown in the question so using `tmin` instead note that the `width=` argument of `rollapply` can be a list containing a vector of offsets (-1 is prior value, -2 is value before that, etc.) and that we can use `rollapplyr` with an `r` on the end to

[R] Take average of previous weeks

2018-03-25 Thread Miluji Sb
Dear all, I have weekly data by city (variable citycode). I would like to take the average of the previous two, three, four weeks (without the current week) of the variable called value. This is what I have tried to compute the average of the two previous weeks; df = df %>% mutate(value.lag1