Опция с использованием data.table
:
setDT(DF)[DF[, .I[which.max(v4)], .(v1, gsub("\\d+$", "", v2))]$V1]
данные:
DF <- structure(list(v1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L), .Label = c("g1", "g3"), class = "factor"), v2 = structure(1:10, .Label = c("A1",
"A2", "A3", "A4", "B1", "B2", "B3", "B4", "C1", "C2"), class = "factor"),
v3 = structure(c(9L, 1L, 7L, 8L, 6L, 10L, 2L, 3L, 5L, 4L), .Label = c("-0.713778796788462",
"-1.08665755420174", "-2.20874190923613", "0.226184032935989",
"0.228687166237024", "0.641078999902449", "0.659864898007848",
"1.11077415422586", "1.30243501910073", "1.57605315010293"
), class = "factor"), v4 = structure(c(6L, 10L, 9L, 1L, 2L,
8L, 4L, 5L, 7L, 3L), .Label = c("-0.206875175346852", "-0.263652840400634",
"-0.563443803887212", "-0.605033653912397", "-1.10776531137661",
"-1.17580991422538", "-1.2693304626523", "-1.27119461040557",
"0.143314307415803", "0.388467088361395"), class = "factor")), class = "data.frame", row.names = c(NA,
-10L))