rearrange columns in dataframe in r code example
Example 1: reorder dataframe cols r
col_order <- c("Species", "Petal.Width", "Sepal.Length",
"Sepal.Width", "Petal.Length")
my_data2 <- my_data[, col_order]
Example 2: r - reorder columns in data frame
# Reorder by column name
df <- df[c("A", "B", "C")]
# Reorder by column index
df <- df[c(1, 3, 2)]