# Rolling Mean and Changing the Type of Target Column

I am trying to calculate the rolling mean (last 20 Periods) of stock returns by a certain ID (called PERMNO). Firstly, I just tried to create the rolling mean. I worked on it with this code:

`CRSP_dt[, rolltest1 := rollmeanr(Return, 20, na.rm = TRUE, fill = NA)]`

which did not work. However, when I try it with this code:

`CRSP_dt[, rolltest2 := rollapplyr(Return, 20, mean, na.rm = TRUE, fill = NA)]`

it actually works, so I am not entirely sure, why?

Secondly, I tried to calculate the rolling mean by PERMNO, so I added the "by" addition:

``````CRSP_dt[, rolltest2 := rollapplyr(Return, 20, mean, na.rm = TRUE, fill = NA),
by = PERMNO]
``````

Now, it returns an error "Error in [.data.table(CRSP_dt, , :=(rolltest2, rollapplyr(Return, : Type of RHS ('logical') must match LHS ('double'). To check and coerce would impact performance too much for the fastest cases. Either change the type of the target column, or coerce the RHS of := yourself (e.g. by using 1L instead of 1)". Since then I have been trying to convert the target column into a "Double" too, by simply using an as.numeric function, resulting in:

``````CRSP_dt[, rolltest2:= as.numeric(rolltest2)]
[, rolltest2 := rollapplyr(Return, 20, mean, na.rm = TRUE, fill = NA),
by = PERMNO]
``````

Unfortunately, it still gives out the same error code, so I am not sure, how to proceed from here.

So for the second problem (th emore relevant one) I tried using:
`CRSP_dt %>% group_by(CRSP_dt\$PERMNO) %>% mutate(rolltest2 = rollapplyr(Return, 10, mean, na.rm = TRUE, fill = NA))`

But unfortunately, it did not work. It does not differentiate between my different PERMNO-IDs, but rather continues calculating the rolling mean.

I think if you use `NA_real_` instead of `NA` for the `fill` argument in your second `data.table` try that should fix at the casting problem.