Thursday, January 5, 2012

Reorder variables in R

Sometimes, variables in a dataframe are in the wrong order. To reorder, you can use the following code:


To order alphabetically
Use data.new <- data[,order(names(data))]

#Example:
names(data)
# [1] "CES_03" "CES_01" "CES_06" "CES_02" "CES_04" "CES_05"
data.new <- data[,order(names(data))]
names(data.new)
# [1] "CES_01" "CES_02" "CES_03" "CES_04" "CES_05" "CES_06"

No comments:

Post a Comment