aqexnix
aqexnix

Reputation: 3

R Iterating through data frame by comparing the previous value in the same column to the next

I have this dataset

enter image description here

Where I want to find out the Cell_Number_~ where the value of column Cell_Length_in_~ doubled in respect to the previous Value. That should be iterative for the Value of Seedling first and then for the condition.

Sorry for maybe being not so clear. I'm very new to this.

Upvotes: 0

Views: 38

Answers (1)

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21440

Here we group_by Seedling to filter out rows where the Cell_Length_in value is at least twice as large as the lagging one:

library(dplyr)
df %>%
  group_by(Seedling) %>%
  filter(
    Cell_Length_in > 2 * lag(Cell_Length_in)
  )
# A tibble: 4 × 3
# Groups:   Seedling [3]
  Seedling Cell_Number_from Cell_Length_in
     <dbl>            <int>          <dbl>
1        1                3           4   
2        2                6          26.7 
3        2                8          24.8 
4        3               12           0.21

Toy data:

df <- data.frame(
  Seedling = c(1,1,1,1,2,2,2,2,3,3,3,3),
  Cell_Number_from = 1:12,
  Cell_Length_in = c(2,1,4,1,
                     12.5,26.7,11.1,24.8,
                     0.2,0.2,0.1,0.21)
)

EDIT:

To retrieve only the first doubled value per group, add slice_head:

df %>%
  group_by(Seedling) %>%
  filter(
    Cell_Length_in > 2 * lag(Cell_Length_in)
  ) %>%
  slice_head()
# A tibble: 3 × 3
# Groups:   Seedling [3]
  Seedling Cell_Number_from Cell_Length_in
     <dbl>            <int>          <dbl>
1        1                3           4   
2        2                6          26.7 
3        3               12           0.21

Upvotes: 1

Related Questions