You can change the order of columns in R with the help of dplyr package from the tidyverse library. Here's an example:
Let's say you have a data frame df
with columns in the following order: "name", "age", "gender".
main.r224 chars10 lines
main.r76 chars5 lines
To change the order of columns in R, you can use the select()
function from dplyr and specify the new order of the columns using their names:
main.r109 chars6 lines
main.r72 chars5 lines
As you can see, the order of columns in the new data frame df_new
is now "gender", "age", "name".
gistlibby LogSnag