user11418708
user11418708

Reputation: 902

Moving data frame columns

Please see below the dataset that I am using for my analyses. HI would like to move 2:25 after col 145.

structure(list(Duration = c(10, 20), 00:00 = c(0, 0), 00:10 = c(0, 0), 00:20 = c(0, 0), 00:30 = c(0, 0), 00:40 = c(0.04, 0.00173913 ), 00:50 = c(0, 0.008695652), 01:00 = c(0, 0), 01:10 = c(0, 0), 01:20 = c(0, 0), 01:30 = c(0, 0), 01:40 = c(0, 0), 01:50 = c(0, 0), 02:00 = c(0, 0), 02:10 = c(0, 0), 02:20 = c(0, 0), 02:30 = c(0, 0), 02:40 = c(0, 0), 02:50 = c(0, 0), 03:00 = c(0.00173913, 0), 03:10 = c(0, 0), 03:20 = c(0, 0), 03:30 = c(0, 0), 03:40 = c(0.005217391, 0.00173913), 03:50 = c(0.00173913, 0), 04:00 = c(0, 0 ), 04:10 = c(0.003478261, 0), 04:20 = c(0, 0), 04:30 = c(0.00173913, 0.00173913), 04:40 = c(0, 0.005217391), 04:50 = c(0.005217391, 0), 05:00 = c(0, 0), 05:10 = c(0, 0), 05:20 = c(0, 0), 05:30 = c(0, 0), 05:40 = c(0, 0), 05:50 = c(0, 0), 06:00 = c(0.00173913, 0), 06:10 = c(0, 0.00173913 ), 06:20 = c(0.00173913, 0), 06:30 = c(0.005217391, 0 ), 06:40 = c(0.003478261, 0.00173913), 06:50 = c(0.00173913, 0.00173913), 07:00 = c(0, 0), 07:10 = c(0.005217391, 0), 07:20 = c(0.003478261, 0), 07:30 = c(0.003478261, 0.00173913), 07:40 = c(0.00173913, 0.00173913), 07:50 = c(0, 0), 08:00 = c(0.003478261, 0), 08:10 = c(0.00173913, 0), 08:20 = c(0.005217391, 0), 08:30 = c(0, 0), 08:40 = c(0, 0.00173913), 08:50 = c(0.003478261, 0), 09:00 = c(0.013913043, 0), 09:10 = c(0.006956522, 0), 09:20 = c(0, 0), 09:30 = c(0.003478261, 0), 09:40 = c(0, 0), 09:50 = c(0, 0.00173913), 10:00 = c(0.00173913, 0.00173913), 10:10 = c(0.005217391, 0.00173913), 10:20 = c(0.00173913, 0.00173913), 10:30 = c(0.00173913, 0.00173913), 10:40 = c(0.00173913, 0), 10:50 = c(0, 0.00173913), 11:00 = c(0.00173913, 0 ), 11:10 = c(0.006956522, 0.00173913), 11:20 = c(0, 0 ), 11:30 = c(0, 0), 11:40 = c(0, 0), 11:50 = c(0, 0 ), 12:00 = c(0.003478261, 0), 12:10 = c(0.003478261, 0), 12:20 = c(0, 0), 12:30 = c(0.00173913, 0), 12:40 = c(0.00173913, 0), 12:50 = c(0, 0), 13:00 = c(0, 0), 13:10 = c(0.005217391, 0), 13:20 = c(0, 0), 13:30 = c(0, 0.00173913), 13:40 = c(0, 0), 13:50 = c(0.005217391, 0.00173913), 14:00 = c(0.003478261, 0.00173913), 14:10 = c(0.00173913, 0), 14:20 = c(0.005217391, 0), 14:30 = c(0.00173913, 0.00173913), 14:40 = c(0.003478261, 0), 14:50 = c(0.00173913, 0), 15:00 = c(0, 0), 15:10 = c(0.005217391, 0.00173913), 15:20 = c(0, 0.00173913), 15:30 = c(0.006956522, 0), 15:40 = c(0.008695652, 0.00173913), 15:50 = c(0.003478261, 0), 16:00 = c(0.00173913, 0), 16:10 = c(0.00173913, 0 ), 16:20 = c(0.003478261, 0.00173913), 16:30 = c(0.00173913, 0), 16:40 = c(0.00173913, 0.00173913), 16:50 = c(0.005217391, 0.006956522), 17:00 = c(0.003478261, 0.00173913), 17:10 = c(0.017391304, 0), 17:20 = c(0.005217391, 0.005217391), 17:30 = c(0.005217391, 0.00173913), 17:40 = c(0.005217391, 0.00173913), 17:50 = c(0.00173913, 0.003478261), 18:00 = c(0.00173913, 0.00173913), 18:10 = c(0.010434783, 0.00173913), 18:20 = c(0.00173913, 0), 18:30 = c(0.00173913, 0.003478261), 18:40 = c(0.00173913, 0), 18:50 = c(0.00173913, 0), 19:00 = c(0.00173913, 0.00173913), 19:10 = c(0.006956522, 0), 19:20 = c(0.005217391, 0.003478261), 19:30 = c(0, 0), 19:40 = c(0, 0), 19:50 = c(0, 0.003478261), 20:00 = c(0.008695652, 0), 20:10 = c(0.006956522, 0.003478261), 20:20 = c(0.003478261, 0), 20:30 = c(0, 0), 20:40 = c(0.008695652, 0), 20:50 = c(0, 0), 21:00 = c(0.00173913, 0), 21:10 = c(0.00173913, 0 ), 21:20 = c(0, 0), 21:30 = c(0, 0), 21:40 = c(0, 0 ), 21:50 = c(0, 0), 22:00 = c(0, 0), 22:10 = c(0, 0 ), 22:20 = c(0, 0), 22:30 = c(0, 0), 22:40 = c(0, 0 ), 22:50 = c(0, 0), 23:00 = c(0, 0), 23:10 = c(0, 0 ), 23:20 = c(0, 0), 23:30 = c(0, 0), 23:40 = c(0, 0 ), 23:50 = c(0, 0)), spec = structure(list(cols = list( Duration = structure(list(), class = c("collector_double", "collector")), 00:00 = structure(list(), class = c("collector_double", "collector")), 00:10 = structure(list(), class = c("collector_double", "collector")), 00:20 = structure(list(), class = c("collector_double", "collector")), 00:30 = structure(list(), class = c("collector_double", "collector")), 00:40 = structure(list(), class = c("collector_double", "collector")), 00:50 = structure(list(), class = c("collector_double", "collector")), 01:00 = structure(list(), class = c("collector_double", "collector")), 01:10 = structure(list(), class = c("collector_double", "collector")), 01:20 = structure(list(), class = c("collector_double", "collector")), 01:30 = structure(list(), class = c("collector_double", "collector")), 01:40 = structure(list(), class = c("collector_double", "collector")), 01:50 = structure(list(), class = c("collector_double", "collector")), 02:00 = structure(list(), class = c("collector_double", "collector")), 02:10 = structure(list(), class = c("collector_double", "collector")), 02:20 = structure(list(), class = c("collector_double", "collector")), 02:30 = structure(list(), class = c("collector_double", "collector")), 02:40 = structure(list(), class = c("collector_double", "collector")), 02:50 = structure(list(), class = c("collector_double", "collector")), 03:00 = structure(list(), class = c("collector_double", "collector")), 03:10 = structure(list(), class = c("collector_double", "collector")), 03:20 = structure(list(), class = c("collector_double", "collector")), 03:30 = structure(list(), class = c("collector_double", "collector")), 03:40 = structure(list(), class = c("collector_double", "collector")), 03:50 = structure(list(), class = c("collector_double", "collector")), 04:00 = structure(list(), class = c("collector_double", "collector")), 04:10 = structure(list(), class = c("collector_double", "collector")), 04:20 = structure(list(), class = c("collector_double", "collector")), 04:30 = structure(list(), class = c("collector_double", "collector")), 04:40 = structure(list(), class = c("collector_double", "collector")), 04:50 = structure(list(), class = c("collector_double", "collector")), 05:00 = structure(list(), class = c("collector_double", "collector")), 05:10 = structure(list(), class = c("collector_double", "collector")), 05:20 = structure(list(), class = c("collector_double", "collector")), 05:30 = structure(list(), class = c("collector_double", "collector")), 05:40 = structure(list(), class = c("collector_double", "collector")), 05:50 = structure(list(), class = c("collector_double", "collector")), 06:00 = structure(list(), class = c("collector_double", "collector")), 06:10 = structure(list(), class = c("collector_double", "collector")), 06:20 = structure(list(), class = c("collector_double", "collector")), 06:30 = structure(list(), class = c("collector_double", "collector")), 06:40 = structure(list(), class = c("collector_double", "collector")), 06:50 = structure(list(), class = c("collector_double", "collector")), 07:00 = structure(list(), class = c("collector_double", "collector")), 07:10 = structure(list(), class = c("collector_double", "collector")), 07:20 = structure(list(), class = c("collector_double", "collector")), 07:30 = structure(list(), class = c("collector_double", "collector")), 07:40 = structure(list(), class = c("collector_double", "collector")), 07:50 = structure(list(), class = c("collector_double", "collector")), 08:00 = structure(list(), class = c("collector_double", "collector")), 08:10 = structure(list(), class = c("collector_double", "collector")), 08:20 = structure(list(), class = c("collector_double", "collector")), 08:30 = structure(list(), class = c("collector_double", "collector")), 08:40 = structure(list(), class = c("collector_double", "collector")), 08:50 = structure(list(), class = c("collector_double", "collector")), 09:00 = structure(list(), class = c("collector_double", "collector")), 09:10 = structure(list(), class = c("collector_double", "collector")), 09:20 = structure(list(), class = c("collector_double", "collector")), 09:30 = structure(list(), class = c("collector_double", "collector")), 09:40 = structure(list(), class = c("collector_double", "collector")), 09:50 = structure(list(), class = c("collector_double", "collector")), 10:00 = structure(list(), class = c("collector_double", "collector")), 10:10 = structure(list(), class = c("collector_double", "collector")), 10:20 = structure(list(), class = c("collector_double", "collector")), 10:30 = structure(list(), class = c("collector_double", "collector")), 10:40 = structure(list(), class = c("collector_double", "collector")), 10:50 = structure(list(), class = c("collector_double", "collector")), 11:00 = structure(list(), class = c("collector_double", "collector")), 11:10 = structure(list(), class = c("collector_double", "collector")), 11:20 = structure(list(), class = c("collector_double", "collector")), 11:30 = structure(list(), class = c("collector_double", "collector")), 11:40 = structure(list(), class = c("collector_double", "collector")), 11:50 = structure(list(), class = c("collector_double", "collector")), 12:00 = structure(list(), class = c("collector_double", "collector")), 12:10 = structure(list(), class = c("collector_double", "collector")), 12:20 = structure(list(), class = c("collector_double", "collector")), 12:30 = structure(list(), class = c("collector_double", "collector")), 12:40 = structure(list(), class = c("collector_double", "collector")), 12:50 = structure(list(), class = c("collector_double", "collector")), 13:00 = structure(list(), class = c("collector_double", "collector")), 13:10 = structure(list(), class = c("collector_double", "collector")), 13:20 = structure(list(), class = c("collector_double", "collector")), 13:30 = structure(list(), class = c("collector_double", "collector")), 13:40 = structure(list(), class = c("collector_double", "collector")), 13:50 = structure(list(), class = c("collector_double", "collector")), 14:00 = structure(list(), class = c("collector_double", "collector")), 14:10 = structure(list(), class = c("collector_double", "collector")), 14:20 = structure(list(), class = c("collector_double", "collector")), 14:30 = structure(list(), class = c("collector_double", "collector")), 14:40 = structure(list(), class = c("collector_double", "collector")), 14:50 = structure(list(), class = c("collector_double", "collector")), 15:00 = structure(list(), class = c("collector_double", "collector")), 15:10 = structure(list(), class = c("collector_double", "collector")), 15:20 = structure(list(), class = c("collector_double", "collector")), 15:30 = structure(list(), class = c("collector_double", "collector")), 15:40 = structure(list(), class = c("collector_double", "collector")), 15:50 = structure(list(), class = c("collector_double", "collector")), 16:00 = structure(list(), class = c("collector_double", "collector")), 16:10 = structure(list(), class = c("collector_double", "collector")), 16:20 = structure(list(), class = c("collector_double", "collector")), 16:30 = structure(list(), class = c("collector_double", "collector")), 16:40 = structure(list(), class = c("collector_double", "collector")), 16:50 = structure(list(), class = c("collector_double", "collector")), 17:00 = structure(list(), class = c("collector_double", "collector")), 17:10 = structure(list(), class = c("collector_double", "collector")), 17:20 = structure(list(), class = c("collector_double", "collector")), 17:30 = structure(list(), class = c("collector_double", "collector")), 17:40 = structure(list(), class = c("collector_double", "collector")), 17:50 = structure(list(), class = c("collector_double", "collector")), 18:00 = structure(list(), class = c("collector_double", "collector")), 18:10 = structure(list(), class = c("collector_double", "collector")), 18:20 = structure(list(), class = c("collector_double", "collector")), 18:30 = structure(list(), class = c("collector_double", "collector")), 18:40 = structure(list(), class = c("collector_double", "collector")), 18:50 = structure(list(), class = c("collector_double", "collector")), 19:00 = structure(list(), class = c("collector_double", "collector")), 19:10 = structure(list(), class = c("collector_double", "collector")), 19:20 = structure(list(), class = c("collector_double", "collector")), 19:30 = structure(list(), class = c("collector_double", "collector")), 19:40 = structure(list(), class = c("collector_double", "collector")), 19:50 = structure(list(), class = c("collector_double", "collector")), 20:00 = structure(list(), class = c("collector_double", "collector")), 20:10 = structure(list(), class = c("collector_double", "collector")), 20:20 = structure(list(), class = c("collector_double", "collector")), 20:30 = structure(list(), class = c("collector_double", "collector")), 20:40 = structure(list(), class = c("collector_double", "collector")), 20:50 = structure(list(), class = c("collector_double", "collector")), 21:00 = structure(list(), class = c("collector_double", "collector")), 21:10 = structure(list(), class = c("collector_double", "collector")), 21:20 = structure(list(), class = c("collector_double", "collector")), 21:30 = structure(list(), class = c("collector_double", "collector")), 21:40 = structure(list(), class = c("collector_double", "collector")), 21:50 = structure(list(), class = c("collector_double", "collector")), 22:00 = structure(list(), class = c("collector_double", "collector")), 22:10 = structure(list(), class = c("collector_double", "collector")), 22:20 = structure(list(), class = c("collector_double", "collector")), 22:30 = structure(list(), class = c("collector_double", "collector")), 22:40 = structure(list(), class = c("collector_double", "collector")), 22:50 = structure(list(), class = c("collector_double", "collector")), 23:00 = structure(list(), class = c("collector_double", "collector")), 23:10 = structure(list(), class = c("collector_double", "collector")), 23:20 = structure(list(), class = c("collector_double", "collector")), 23:30 = structure(list(), class = c("collector_double", "collector")), 23:40 = structure(list(), class = c("collector_double", "collector")), 23:50 = structure(list(), class = c("collector_double", "collector"))), default = structure(list(), class = c("collector_guess", "collector")), skip = 1), class = "col_spec"), row.names = 1:2, class = "data.frame")

Upvotes: 0

Views: 62

Answers (3)

Chuck P
Chuck P

Reputation: 3923

A dplyr solution

library(dplyr)
relocate(df, 2:25, .after = 145)

Almost reads exactly like your question :)

To generically move them to the end of the columns no matter how many columns

df %>% relocate(2:25, .after = last_col())

Upvotes: 1

peter
peter

Reputation: 786

This should be more robust. Meaning it would produce the same results even when the number of columns change (especially when it grows):

df_new <- cbind(df[, 1:145], df[, 2:25], df[, 146:ncol(df)]) # move a duplicate of the columns in the right position
df_new <- df_new[, -2:-25] # delete columns from original positions

Upvotes: 2

ekoam
ekoam

Reputation: 8844

Try this

cbind(df[, -2:-25], df[, 2:25])

Upvotes: 2

Related Questions