Sort a data frame when column name is assigned in a variable I am new to R and hence my question is likely to be basic. I tried researching the answer before posting here however didn't get the answer I was looking for.

I am trying to order a data frame on the basis of certain columns. The column is dynamic and depends on a certain "outcome" which would be from a vector of "validoutcomes".

validoutcomes &lt;- c("A","B","C")

if outcome is "A" then I am supposed to read data from column 11. For "B" its columns 17 and for "C" it is 23.

I renamed the columns so that it is easier for my readability and alignment with  validoutcomes. "input" is my source data from the CSV.

colnames(input)[11] &lt;- validoutcomes[1]
 colnames(input)[17] &lt;- validoutcomes[2]
 colnames(input)[23] &lt;- validoutcomes[3]

I then tried to sort using

statelist &lt;- statelist[order(statelist$outcome)]

Any ideas?
Your first attempt was more accurate. Since I do not have your data I will just use the iris data set,

 iris_ordered_on_Sepal_Length &lt;- iris[order(iris$Sepal.Length),]


or if you use data.table,

iris_dt &lt;- data.table(iris)
iris_dt[order(Sepal.Length)]


data.table will make renaming easier too,

setnames(input, c("colname_11, "colname_17", "colname_23"), validoutcomes[1 : 3])