I have a very wide data.frame in R. In order to make it easier to read, I'd like the variables in a certain order. ID NAME GENDER EVERYTHING ELSE (ordered by alpha)
The below works, but seems clunky and I need something that can generalize ( i have more than 3 columns i need to set at the beginning). Is there an easier way to accomplish this or am I stuck doing it this way?
# Columns
A <- c(100, 200, 100)
name <- c("Tim", "Jim", "Kim")
C <- c(2, 2, 1)
ID <- c(110, 111, 112)
B <- c(300, 330, 320)
gender = c("M", "M", "F")
# Data.frame
have <- data.frame(A, name, C, ID, B, gender)
# Alpha
want <- have[, order(names(have))]
# Move columns
want <- want[,c(which(colnames(want)=="gender"), which(colnames(want)!="gender"))]
want <- want[,c(which(colnames(want)=="name"), which(colnames(want)!="name"))]
want <- want[,c(which(colnames(want)=="id"), which(colnames(want)!="id"))]