reorder multiple data columns so oldest is on the left and newest is on the right in r

You can reorder multiple data columns in R so that the oldest is on the left and the newest is on the right using the select function along with the everything() function from the dplyr package.

Here is an example code snippet showing how to reorder the columns:

library(dplyr)

# Create a sample dataframe
df <- data.frame(ID = 1:3, Name = c("Alice", "Bob", "Charlie"), Age = c(25, 30, 22))

# Reorder columns with oldest on the left and newest on the right
df <- df %>% select(1, ncol(df):2)

# Print the dataframe with reordered columns
print(df)
287 chars
11 lines

In the example above, columns are reordered so that the first column remains the same (ID), the newest column "Age" is on the right, and the intermediate columns are shifted according to the new order.

This will result in the columns being reordered with the oldest on the left and the newest on the right in the dataframe.

related categories

gistlibby LogSnag