2

I have a column of numbers (index) in a dataframe like the below. I am attempting to check if these numbers are in ascending order by the value of 1. For example, group B and C do not ascend by 1. While I can check by sight, my dataframe is thousands of rows long, so I'd prefer to automate this. Does anyone have advice? Thank you!

group index

A     0
A     1
A     2
A     3
A     4
B     0
B     1
B     2
B     2
C     0
C     3
C     1
C     2
...

psychcoder
  • 447
  • 1
  • 7

3 Answers3

3

I think this works. diff calculates the difference between the two subsequent numbers, and then we can use all to see if all the differences are 1. dat2 is the final output.

library(dplyr)

dat2 <- dat %>%
  group_by(group) %>%
  summarize(Result = all(diff(index) == 1)) %>%
  ungroup()
dat2
# # A tibble: 3 x 2
#   group Result
#   <chr> <lgl> 
# 1 A     TRUE  
# 2 B     FALSE 
# 3 C     FALSE 

DATA

dat <- read.table(text = "group index
A 0
A 1
A 2
A 3
A 4
B 0
B 1
B 2
B 2
C 0
C 3
C 1
C 2",
                  header = TRUE, stringsAsFactors = FALSE)
www
  • 35,154
  • 12
  • 33
  • 61
2

Maybe aggregate could help

> aggregate(.~group,df1,function(v) all(diff(v)==1))
  group index
1     A  TRUE
2     B FALSE
3     C FALSE
ThomasIsCoding
  • 53,240
  • 4
  • 13
  • 45
1

We can do a group by group, get the difference between the current and previous value (shift) and check if all the differences are equal to 1.

library(data.table)
setDT(df1)[, .(Result = all((index - shift(index))[-1] == 1)), group]
#   group Result
#1:     A   TRUE
#2:     B  FALSE
#3:     C  FALSE

data

df1 <- structure(list(group = c("A", "A", "A", "A", "A", "B", "B", "B", 
"B", "C", "C", "C", "C"), index = c(0L, 1L, 2L, 3L, 4L, 0L, 1L, 
2L, 2L, 0L, 3L, 1L, 2L)), class = "data.frame", row.names = c(NA, 
-13L))
akrun
  • 674,427
  • 24
  • 381
  • 486