0

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))

Original data frame

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?

gillyp
  • 25
  • 4
  • Take the code from the dupe link and replace `mean` with `max` and it'll work for you, For example, `aggregate( x3 ~ x2, data, max )`. – r2evans Sep 15 '22 at 18:45
  • It is similar but not what I'm looking for, say you flip the 1 and 9 in the As, then the output should give me 1 which is not the biggest value. Thanks though! – gillyp Sep 15 '22 at 19:02
  • Okay, so ... last value only? How about `aggregate(x3 ~ x2, data, tail, n=1)`? More verbosely (but perhaps helpful): `aggregate(x3 ~ x2, data=data, FUN=function(z) tail(z, n=1))`. The point is that you need to do "something" (max, mean, first, last, sample, whatever), and do it once "per group". For that, the functions in the dupe link show various ways to do that, including `tapply` (not good here), `aggregate`, `dplyr::group_by`, `split`/`lapply`, etc. – r2evans Sep 15 '22 at 19:03
  • Does that clear it up? – r2evans Sep 15 '22 at 19:06

0 Answers0