Search code examples
rdataframelogical-operatorsunique-index

R function to identify unique rows from previous rows, not within the dataframe altogether


I have a dataframe in which I need to identify or index the start of each new trial. A new trial is indicated by variable Location from 0-8. Example below:

    zPos        Location
    1.9148150   6
    1.914815    6
    1.914815    6
    1.914815    6
    1.914815    6
    0.9018518   3
    0.9018518   3
    0.9009259   3
    0.9009259   3
    0.9009259   3
    0.9009259   3

There are 72 trials in each dataframe, so each location value repeats 8 times meaning unique won't work. I am a novice when it comes to R, so I haven't tried much outside of base R and dplyr to tackle this problem.

Ideally I would like to create a new variable for trial number, example below:

    zPos        Location       TrialNum
    1.9148150   6              1
    1.914815    6              1
    1.914815    6              1
    1.914815    6              1
    1.914815    6              1
    0.9018518   3              2
    0.9018518   3              2
    0.9009259   3              2
    0.9009259   3              2
    0.9009259   3              2
    0.9009259   3              2

But I could also work with an index of the starting location for each new trial rather than a new variable in the dataframe.

This is my first question on stackoverflow, so I greatly appreciate any assistance or insight.


Solution

  • You could use rle to do this.

    df <- data.frame(
      zPos = c(1.9148150, 1.914815, 1.914815, 1.914815, 1.914815, 0.9018518,
               0.9018518, 0.9009259, 0.9009259, 0.9009259, 0.9009259),
      Location = c(6, 6, 6, 6, 6, 3, 3, 3, 3, 3, 3)
    )
    
    get_trial <- function(col) {
      r <- rle(col)
      rep(seq(length(r$lengths)), r$lengths)
    }
    
    df %>%
      mutate(TrialNum = get_trial(Location))
    
            zPos Location TrialNum
    1  1.9148150        6        1
    2  1.9148150        6        1
    3  1.9148150        6        1
    4  1.9148150        6        1
    5  1.9148150        6        1
    6  0.9018518        3        2
    7  0.9018518        3        2
    8  0.9009259        3        2
    9  0.9009259        3        2
    10 0.9009259        3        2
    11 0.9009259        3        2