Is there a base R function to dynamically order data.frame columns similar to dplyr everything()?
You can use setdiff
:
df1[, c(keep1, setdiff(names(df1), keep1))]
df1[order(match(names(df1), keep1, nomatch = NCOL(df1) + 1))]
# id month year v1 v2 v3
#1 1 jan 1999 A 2 10
#2 2 jan 2001 B 2 10
#3 3 mar 1984 C 2 10
#4 4 oct 1979 D 2 10
#5 5 dec 2019 E 2 10
Another option
df1[, unique(c(which(names(df1) %in% keep1), seq_along(df1)))]
or
df1[, unique(c("id", "month", "year", names(df1)))]