Suppose we have a data frame
> (df <- as.data.frame(matrix(1:12, 3, 4)))
V1 V2 V3 V4
1 1 4 7 10
2 2 5 8 11
3 3 6 9 12
Is there a way to let which() throw column numbers in an unordered form? I. e. which(names(df) %in% c("V1", "V4", "V3")) yielding 1, 4, 3 instead of 1, 3, 4? Or, if there isn't, how can we achieve this most easily?