I assume you know the years and the number of observations per year is pretty close to the same each year.
df <- data.frame(
year=c(2019,2020,2021,2022,2019,2020,2021,2022,2019,2020,2021,2022),
sales=c(1,2,3,4,5,6,7,8,9,10,11,12))
set.seed(1234)
s <- sample(df$year, size = 2, replace = FALSE)
train <- df[df$year %in% s,]
train
test <- df[!(df$year %in% s),]
test