I have a dataframe in R like the following, I want to move the columns. For example, bring the third column to the first column and vice versa. Can you please help me with that? For example I want to build new df with columns " datathonpresevative, datathoncountry, datathoncoordinates, and so on".
Asked
Active
Viewed 43 times
0
-
Hi Mostafa Alaverdi, you are in fact looking for "How does one reorder columns in a data frame?", see this above SO question for answers and [how-to-make-a-great-r-reproducible-example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to improve your reproducible examples. – cbo Sep 01 '22 at 07:23
1 Answers
0
library(tidyverse)
# Move the third column to first
df %>%
relocate(3)
# Subset columns
df %>%
select(datathonpresevative, datathoncountry, datathoncoordinates)

Chamkrai
- 5,912
- 1
- 4
- 14