Search code examples
rdifference

compute the difference of two values within 1 column


I have a big table of total 276 rows and I need to find the difference between every two rows in 1 column e.g. row1 and row2, row3 and row4, row5 and row6 etc. How can I do that? I was told to do it by the command diff() but I have no idea where to start.

|subject/condition/response time/ **difference (what I want)**

| Jef      | A              | 1000sec         | **2000**
            
| Jef      | B              | 3000sec         | **2000**

|Amy       | A              | 2000sec         | **11000**

|Amy       | B              | 13000 sec       | **11000**

|Edan      | A              | 1500 sec        | **300**

|Edan      | B              | 1800 sec        | **300**

Solution

  • The solution is quite straightforward iff, as your sample suggests, you always have 2 values for each subject:

    library(dplyr)
    df %>%
      group_by(Subject) %>%
      mutate(Diff = lead(Response_time) - Response_time) %>%
      fill(Diff)
    # A tibble: 6 × 3
    # Groups:   Subject [3]
      Subject Response_time  Diff
      <chr>           <dbl> <dbl>
    1 Jeff             1000  2000
    2 Jeff             3000  2000
    3 Amy              2000 11000
    4 Amy             13000 11000
    5 Ed               1500   300
    6 Ed               1800   300
    

    Data:

    df <- data.frame(
      Subject = c("Jeff","Jeff","Amy","Amy","Ed","Ed"),
      Response_time = c(1000,3000,2000,13000,1500,1800) 
    )