I would join the data frame to itself as in the following code.
library(dplyr)
DF <- structure(list(SAMPN = c(1L, 1L, 4L, 4L), PERNO = c(1, 2, 1,2),
overlap = c("2.0", "1.0", "2.0", "1.0"),
utility = 1:4), row.names = c(1L, 2L, 6L, 9L),
class = "data.frame")
DF <- DF %>% mutate(overlap = as.numeric(overlap))
Joined <- inner_join(DF, DF, by = c(SAMPN = "SAMPN", "overlap" = "PERNO"))
Joined
#> SAMPN PERNO overlap utility.x overlap.y utility.y
#> 1 1 1 2 1 1 2
#> 2 1 2 1 2 2 1
#> 3 4 1 2 3 1 4
#> 4 4 2 1 4 2 3
Joined <- Joined %>% mutate(indicator = ifelse(utility.x > utility.y, 1, 0)) %>%
select(SAMPN, PERNO, overlap, utility = utility.x, indicator)
Joined
#> SAMPN PERNO overlap utility indicator
#> 1 1 1 2 1 0
#> 2 1 2 1 2 1
#> 3 4 1 2 3 0
#> 4 4 2 1 4 1
Created on 2019-09-09 by the reprex package (v0.2.1)
Edited the inner_join conditions, which I had backwards, though they happened to work in this simple case.