reorder columns in r

To reorder columns in an R data frame, you can use the select function from the dplyr package. Here's an example:

main.r
library(dplyr)

# create a sample data frame
data <- data.frame(A = 1:5, B = 6:10, C = 11:15)

# reorder the columns
data_reorder <- select(data, C, A, B)
155 chars
8 lines

In the above example, we first load the dplyr package. We then create a sample data frame called "data" with three columns: A, B, and C. To reorder the columns, we use the select function and specify the new column order as arguments. In this case, we want to reorder the columns so that C comes first, followed by A, and then B. We assign the result to a new data frame called "data_reorder". The resulting data frame will have the same data as the original data frame, but with the columns reordered.

related categories

gistlibby LogSnag