I was wondering if there is a easier way to rearrange the columns in a data frame without writing the index number of every column. Index numbers are fine when its a small data frame but when its big writing the index numbers would be very repetitive.
I have a data frame called pd, which is
structure(list(X1 = c(6, 1), X2 = c(7, 2), X3 = c(8, 3), X4 = c(9,
4), X5 = c(10, 5), X6 = c(11, 6), X7 = c(12, 7), X8 = c(13, 8
), X9 = c(14, 9), X10 = c(15, 10), X11 = c(16, 11), X12 = c(17,
12), X13 = c(18, 13), X14 = c(19, 14), X15 = c(20, 15), X16 = c(21,
16), X17 = c(22, 17), X18 = c(23, 18), X19 = c(24, 19), X20 = c(25,
20), X21 = c(26, 21), X22 = c(27, 22), X23 = c(28, 23), X24 = c(29,
24), X25 = c(30, 25), X26 = c(31, 26), X27 = c(32, 27), X28 = c(33,
28), X29 = c(34, 29), X30 = c(35, 30), X31 = c(36, 31), X32 = c(37,
32), X33 = c(38, 33), X34 = c(39, 34), X35 = c(40, 35), X36 = c(41,
36), X37 = c(42, 37), X38 = c(43, 38), X39 = c(44, 39), X40 = c(45,
40), X41 = c(46, 41), X42 = c(47, 42), X43 = c(48, 43), X44 = c(49,
44), X45 = c(50, 45), X46 = c(51, 46), X47 = c(52, 47), X48 = c(53,
48), X49 = c(54, 49), X50 = c(55, 50), X51 = c(56, 51), X52 = c(57,
52)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-2L))
The expected output is to bring the column X52 into the first column space. If anyone has any idea could you please share it with me as I couldn't find one while searching in internet.
Attached is the image of the output:
CodePudding user response:
Use [
to reorder columns:
df[c(52, 1:51)]
df[c("X52", colnames(df)[-52])]
Or more programmatically:
df[c(ncol(df), seq(ncol(df) - 1))]
Or, with dplyr::relocate
:
library(dplyr)
relocate(df, 52)
relocate(df, "X52")
More programmatically:
relocate(df, ncol(df))
CodePudding user response:
Use dplyr::relocate()
. You can tidy-select
columns you want to choose by their column number.
library(tidyverse)
pd <- structure(list(X1 = c(6, 1), X2 = c(7, 2), X3 = c(8, 3), X4 = c(9,
4), X5 = c(10, 5), X6 = c(11, 6), X7 = c(12, 7), X8 = c(13, 8
), X9 = c(14, 9), X10 = c(15, 10), X11 = c(16, 11), X12 = c(17,
12), X13 = c(18, 13), X14 = c(19, 14), X15 = c(20, 15), X16 = c(21,
16), X17 = c(22, 17), X18 = c(23, 18), X19 = c(24, 19), X20 = c(25,
20), X21 = c(26, 21), X22 = c(27, 22), X23 = c(28, 23), X24 = c(29,
24), X25 = c(30, 25), X26 = c(31, 26), X27 = c(32, 27), X28 = c(33,
28), X29 = c(34, 29), X30 = c(35, 30), X31 = c(36, 31), X32 = c(37,
32), X33 = c(38, 33), X34 = c(39, 34), X35 = c(40, 35), X36 = c(41,
36), X37 = c(42, 37), X38 = c(43, 38), X39 = c(44, 39), X40 = c(45,
40), X41 = c(46, 41), X42 = c(47, 42), X43 = c(48, 43), X44 = c(49,
44), X45 = c(50, 45), X46 = c(51, 46), X47 = c(52, 47), X48 = c(53,
48), X49 = c(54, 49), X50 = c(55, 50), X51 = c(56, 51), X52 = c(57,
52)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-2L))
pd2 <- relocate(.data = pd, X52, .before = 1)
pd2
# A tibble: 2 × 52
X52 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 57 6 7 8 9 10 11 12 13 14 15 16 17
2 52 1 2 3 4 5 6 7 8 9 10 11 12
# … with 39 more variables: X13 <dbl>, X14 <dbl>, X15 <dbl>, X16 <dbl>,
# X17 <dbl>, X18 <dbl>, X19 <dbl>, X20 <dbl>, X21 <dbl>, X22 <dbl>,
# X23 <dbl>, X24 <dbl>, X25 <dbl>, X26 <dbl>, X27 <dbl>, X28 <dbl>,
# X29 <dbl>, X30 <dbl>, X31 <dbl>, X32 <dbl>, X33 <dbl>, X34 <dbl>,
# X35 <dbl>, X36 <dbl>, X37 <dbl>, X38 <dbl>, X39 <dbl>, X40 <dbl>,
# X41 <dbl>, X42 <dbl>, X43 <dbl>, X44 <dbl>, X45 <dbl>, X46 <dbl>,
# X47 <dbl>, X48 <dbl>, X49 <dbl>, X50 <dbl>, X51 <dbl>
# ℹ Use `colnames()` to see all variable names