2
votes

Hi I have a dataframe as such.

df= structure(list(x = c(116, 116, 176, 176, 367, 367, 367), order = c("1", "2", "1", "2", "1", "2", "3")
            , value = c(8.6, 9.23, 8.9, 9.06,9.57, 10.48, 11.3))
            , row.names = c("1", "2", "3", "4", "5","6", "7"), class = "data.frame")

    x order value
1 116     1  8.60
2 116     2  9.23
3 176     1  8.90
4 176     2  9.06
5 367     1  9.57
6 367     2 10.48
7 367     3 11.30

What I like to do, without looping, is get the difference of the last and first temporal order of each of the unique x sample. For example, 367 should be 1.73. I tried using dplyr with lag but I can't seem to figure out how to make sure that difference is only between last and first.

df %>% 
 dplyr::group_by(x) %>% 
 dplyr::mutate(diff = value - lag(value)) %>%  data.frame()

This yields zero diff for all x.

thanks.

2

2 Answers

2
votes

Just use first() and last()

df %>% 
 dplyr::group_by(x) %>% 
 dplyr::mutate(diff = last(value) - first(value)) %>%  data.frame()
1
votes

If it is already ordered, then we can use diff(range)

library(dplyr)
df %>% 
    group_by(x) %>%
     mutate(diff = diff(range(value)))