Here is a simple example:
> df <- data.frame(sn=rep(c("a","b"), 3), t=c(10,10,20,20,25,25), r=c(7,8,10,15,11,17))
> df
sn t r
1 a 10 7
2 b 10 8
3 a 20 10
4 b 20 15
5 a 25 11
6 b 25 17
Expected result is
sn t r
1 a 20 3
2 a 25 1
3 b 20 7
4 b 25 2
I want to group by a specific column ("sn"), leave some columns unchanged ("t" for this example), and apply diff() to remaining columns ("r" for this example). I explored "dplyr" package to try something like:
df1 %>% group_by(sn) %>% do( ... diff(r)...)
but couldn't figure out correct code. Can anyone recommend me a clean way to get expected result?