-
Notifications
You must be signed in to change notification settings - Fork 14
Open
Description
Would it be possible to implement backwards rolling. Thus, it would be a very convenient way to implement multi periods condition when handling panel data.
library(tidyverse)
#> + ggplot2 2.2.1 Date: 2017-05-29
#> + tibble 1.3.3 R: 3.3.2
#> + tidyr 0.6.3 OS: Windows 10 x64
#> + readr 1.1.1 GUI: RTerm
#> + purrr 0.2.2.2 Locale: Danish_Denmark.1252
#> + dplyr 0.5.0 TZ: Europe/Paris
#> + stringr 1.2.0
#> + forcats 0.2.0
#> Warning: package 'ggplot2' was built under R version 3.3.3
#> Warning: package 'tibble' was built under R version 3.3.3
#> Warning: package 'tidyr' was built under R version 3.3.3
#> Warning: package 'readr' was built under R version 3.3.3
#> Warning: package 'purrr' was built under R version 3.3.3
#> Warning: package 'dplyr' was built under R version 3.3.3
#> Warning: package 'forcats' was built under R version 3.3.3
#> -- Conflicts ----------------------------------------------------
#> * filter(), from dplyr, masks stats::filter()
#> * lag(), from dplyr, masks stats::lag()
library(RcppRoll)
#> Warning: package 'RcppRoll' was built under R version 3.3.3
df <- tibble(id = rep(1, each = 10), wage = c(980, rep(1000, each = 9)))
df %>%
mutate(wagecri = if_else(wage >= 1000, 1, 0)) %>%
mutate(crimean = roll_meanr(wagecri, n = 2))
#> # A tibble: 10 x 4
#> id wage wagecri crimean
#> <dbl> <dbl> <dbl> <dbl>
#> 1 1 980 0 NA
#> 2 1 1000 1 0.5
#> 3 1 1000 1 1.0
#> 4 1 1000 1 1.0
#> 5 1 1000 1 1.0
#> 6 1 1000 1 1.0
#> 7 1 1000 1 1.0
#> 8 1 1000 1 1.0
#> 9 1 1000 1 1.0
#> 10 1 1000 1 1.0
# I would like the result to be as follows
tibble(id = rep(1, each = 10), wage = c(980, rep(1000, each = 9))) %>%
mutate(wagecri = if_else(wage >= 1000, 1, 0)) %>%
mutate(crimean = c(0.5, rep(1, each = 8), NA_real_))
#> # A tibble: 10 x 4
#> id wage wagecri crimean
#> <dbl> <dbl> <dbl> <dbl>
#> 1 1 980 0 0.5
#> 2 1 1000 1 1.0
#> 3 1 1000 1 1.0
#> 4 1 1000 1 1.0
#> 5 1 1000 1 1.0
#> 6 1 1000 1 1.0
#> 7 1 1000 1 1.0
#> 8 1 1000 1 1.0
#> 9 1 1000 1 1.0
#> 10 1 1000 1 NABest regards,
Jakob
Metadata
Metadata
Assignees
Labels
No labels