Hi I am trying to find a leave one out average of a variable in all rows using dplyr
. Since dplyr
provides a convenient function called row_number()
, I thought I could use it like this:
library(dplyr)
iris %>%
tbl_df %>%
select(Sepal.Length) %>%
mutate(loo_avg=mean(Sepal.Length[-row_number()])) # leave one out average
But this returns a result like this:
Source: local data frame [150 x 2]
Sepal.Length loo_avg
(dbl) (dbl)
1 5.1 NaN
2 4.9 NaN
3 4.7 NaN
4 4.6 NaN
5 5.0 NaN
6 5.4 NaN
7 4.6 NaN
8 5.0 NaN
9 4.4 NaN
10 4.9 NaN
.. ... ...
How do you fix this?