Reorder Columns of Data Frame in R (4 Examples) | Change Position of Variables | subset & select

preview_player
Показать описание
R code of this video:

x2 = LETTERS[1:5],
x3 = c(2, 4, 6, 8, 0))
data # Print example data

##### Example 1
data[ , c(2, 1, 3)] # Reorder columns by index

##### Example 2
data[ , c("x2", "x1", "x3")] # Reorder columns by names

##### Example 3
subset(data, select = c(2, 1, 3)) # Reorder columns with subset()

##### Example 4
library("dplyr") # Load dplyr package
data %>% select(x2, x1, x3) # Reorder columns with select()
Рекомендации по теме
Комментарии
Автор

For people who are beginners with R, this will not work until you assign it to you your data frame eg: data <- data [, c(2, 1, 3)]. Took me a while to figure out haha, but thanks for the video.

biusschehl
Автор

Thanks you for your videos!

I have still one question. In my case the data object still shows the previous position of the coloumns unlike the dataframe, how can the data object also be switched into the new order?

samuelklos
Автор

What if I have 200 columns in a data frame, and I just want to shift 155th column to 175th column position for example?

mashrursk.md.
Автор

Is it possible in other way using tidyverse package?

vicvic
join shbcf.ru