I want to create a stop() for my data.frame below such that for each unique id, if pos is varying (e.g., consisting of 1s, 2s,etc.), then, if mp values for the rows for which cont==TRUE are not the same, we should throw an error.
Is this possible in R?
In the below toy example, id == "B" should throw an error because, pos is varying (1,2,3), and mp values (i.e., 1,3) for the rows for which cont==TRUE are not the same.
dat <- data.frame(id = rep(c("A","B"),2:3), mp = c(1,5, 2,1,3), cont = c(F,T, F,T,T), pos = c(1,1, 1:3))
# id mp cont pos
#1 A 1 FALSE 1
#2 A 5 TRUE 1
#3 B 2 FALSE 1
#4 B 1 TRUE 2
#5 B 3 TRUE 3
# Desired stop() message:
"Error: 'B' has a wrong value."