В базе R мы можем использовать ave
для подсчета количества строк в каждой группе значений first
и last
и выбора групп, в которых количество строк равно 6.
subset(df, ave(start_year, first, last, FUN = length) == 6)
# last first start_year end_year Team GP MIN PTS W L
#4 Acy Quin… 2013 2014 SAC 63 13.5 2.7 22 41
#5 Acy Quin… 2014 2015 NYK 68 18.9 5.9 12 56
#6 Acy Quin… 2015 2016 SAC 59 14.8 5.3 21 38
#7 Acy Quin… 2016 2017 BKN 38 14.7 5.8 11 27
#8 Acy Quin… 2017 2018 BKN 70 19.4 5.9 26 44
#9 Acy Quin… 2018 2019 PHX 10 12.3 1.7 2 8
Мы можно сделать то же самое с data.table
library(data.table)
setDT(df)[,.SD[.N == 6], .(first, last)]
data
df <- structure(list(last = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("Abri…", "Acy"), class = "factor"), first = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Alex", "Quin…"
), class = "factor"), start_year = c(2016L, 2017L, 2018L, 2013L,
2014L, 2015L, 2016L, 2017L, 2018L), end_year = c(2017L, 2018L,
2019L, 2014L, 2015L, 2016L, 2017L, 2018L, 2019L), Team = structure(c(3L,
3L, 3L, 5L, 2L, 5L, 1L, 1L, 4L), .Label = c("BKN", "NYK", "OKC",
"PHX", "SAC"), class = "factor"), GP = c(68L, 75L, 31L, 63L,
68L, 59L, 38L, 70L, 10L), MIN = c(15.5, 15.1, 19, 13.5, 18.9,
14.8, 14.7, 19.4, 12.3), PTS = c(6, 4.8, 5.3, 2.7, 5.9, 5.3,
5.8, 5.9, 1.7), W = c(37L, 42L, 21L, 22L, 12L, 21L, 11L, 26L,
2L), L = c(31L, 33L, 10L, 41L, 56L, 38L, 27L, 44L, 8L)), class = "data.frame",
row.names = c(NA, -9L))