Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roll backwards #27

Open
elben10 opened this issue May 29, 2017 · 0 comments
Open

Roll backwards #27

elben10 opened this issue May 29, 2017 · 0 comments

Comments

@elben10
Copy link

elben10 commented May 29, 2017

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      NA

Best regards,
Jakob

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant