Say we have a data frame (example/pictures provided below) and you want to make a loop that looks at each row on "x2", and if the next item on "x2" is different from the previous one, you grab that previous item and its corresponding "x3" value and add it to a different data frame. EDIT: The last number does not have to be the biggest number in the group, it just has to be the latest one. Example data frame below:
data <- data.frame(x1 = 1:9,
x2 = c("a","a","a",'b','b','c','d','d','e'),
x3 = c(4, 1, 9, 0, 1, 2, 3, 8, 4))
And the output for the new data frame would then be: Output after running the loop
Does anyone know how this can be done or have any links to similar problems?