Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

dplyr function on grouped data to get the value of a variable when a different variable is equal to its min value

I have the following dataset:

id <- c(1,1,1,2,2,2)
year <- c(2012, 2013, 2014, 2012, 2013, 2014)
assignment <- c(0,1,1,0,0,0)
df <- cbind.data.frame(id, year, assignment)

I want to mutate a new variable which takes the value of whatever the assignment variable was equal to in the minimum year value per group. For example, for ID 1, the value of assignment in the smallest year (2012) is 0, so the new variable will be zero in all instances of ID 1.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could group_by id, arrange by year and get the first value of assignment:

library(dplyr, warn=FALSE)

df %>% 
  group_by(id) %>% 
  arrange(year) %>% 
  mutate(new_assignment = first(assignment)) %>% 
  ungroup()
#> # A tibble: 6 × 4
#>      id  year assignment new_assignment
#>   <dbl> <dbl>      <dbl>          <dbl>
#> 1     1  2012          0              0
#> 2     2  2012          0              0
#> 3     1  2013          1              0
#> 4     2  2013          0              0
#> 5     1  2014          1              0
#> 6     2  2014          0              0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading