Conditional and grouped mutate dplyr

Viewed 315

Lets say I have the following data of sock increase per drawer

>socks
year  drawer_nbr  sock_total
1990    1           2
1991    1           2
1990    2           3
1991    2           4
1990    3           2
1991    3           1

I would like to have a binary variable that identifies if the socks have increased in each drawer. 1 if they increased and 0 if not. The result would be

>socks
drawer_nbr  growth
  <dbl>     <factor>
    1          0  
    2          1
    3          0

I am getting stuck on comparing sock_total of one year vs sock_total of another year. I know that I need to use dplyr::summaries(), but I am having difficulty with what goes inside that function.

2 Answers
Related