I think the easiest approach is to construct the test and training populations by sampling the group column. Let's say your data are in a data frame named DF, there are ten groups labeled 1 - 10 and you want the training sample to be 7 of the groups.
library(dplyr)
training <- sample(1:10, 7, replace = FALSE)
training
TrnDF <- DF %>% filter(group %in% training)
TestDF <- DF %>% filter(!group %in% training)
That may cause a problem if the groups are of very different sizes, but that is inherent in the data.