How to change the order of appearance of Columns in a Data Frame?

Asked

Viewed 4,556 times

4

I’ve got this df mounted on the R:

                  Produto  Classificação Comun Quilo Indice
1           ABACAXI HAVAI       A GRAUDO  3,32   2,2      2
2           ABACAXI HAVAI        B MEDIO  2,81   1,8      3
3           ABACAXI HAVAI        C MIUDO  2,21   1,4      4
4             BANANA MACA              -   4,5     1      5
5         BANANA PRATA MG              -  3,13     1      6
6         BANANA PRATA SP              -  2,82     1      7
7   BANANA NANICA CLIMAT.              -  2,16     1     11
8              COCO VERDE              -  2,09   2,5      8
9            LARANJA LIMA    A (9/12 DZ)  2,46     1     12
10           LARANJA LIMA   B (13/15 DZ)  2,16     1     13
11           LARANJA LIMA   C (16/21 DZ)  1,97     1     14
12           LARANJA PERA    A (9/12 DZ)  2,84     1     15
13           LARANJA PERA   B (13/15 DZ)  2,63     1     16
14  MACA ESTRANG. RED DEL  80-163 FRUTOS  5,37     1     10
15     MACA NACIONAL FUJI 163-180 FRUTOS  5,31     1     17
16          MAMAO FORMOSA              A  2,99     1     18
17               UVA RUBI       ESPECIAL  3,48     1      9
18               MANDIOCA          MEDIA  1,83     1     19
21 TOMATE ACHATADO-REDOND        EXTRA A     2     1     20
19        BATATA ESCOVADA       ESPECIAL  1,78     1     21

I would like to put the "Index" column before the Product column, and leave the rest in the same order, as I can do this?

I want it to stay that way:

   Indice             Produto  Classificação Comun Quilo 
1   2           ABACAXI HAVAI       A GRAUDO  3,32   2,2      
2   3           ABACAXI HAVAI        B MEDIO  2,81   1,8      
3   4           ABACAXI HAVAI        C MIUDO  2,21   1,4      
4   5             BANANA MACA              -   4,5     1      
5   6         BANANA PRATA MG              -  3,13     1      
6   7         BANANA PRATA SP              -  2,82     1      
7   11  BANANA NANICA CLIMAT.              -  2,16     1     
8   8              COCO VERDE              -  2,09   2,5     
9   12           LARANJA LIMA    A (9/12 DZ)  2,46     1     
10  13           LARANJA LIMA   B (13/15 DZ)  2,16     1     
11  14           LARANJA LIMA   C (16/21 DZ)  1,97     1     
12  15           LARANJA PERA    A (9/12 DZ)  2,84     1     
13  16           LARANJA PERA   B (13/15 DZ)  2,63     1     
14  10  MACA ESTRANG. RED DEL  80-163 FRUTOS  5,37     1     
15  17     MACA NACIONAL FUJI 163-180 FRUTOS  5,31     1     
16  18          MAMAO FORMOSA              A  2,99     1     
17  9                UVA RUBI       ESPECIAL  3,48     1     
18  19               MANDIOCA          MEDIA  1,83     1     
21  20 TOMATE ACHATADO-REDOND        EXTRA A     2     1     
19  21        BATATA ESCOVADA       ESPECIAL  1,78     1     

4 answers

2


Assuming the data frame is called df:

library(dplyr)
df_ordenado <- df %>%
  select(Indice, Produto, Classificação, Comun, Quilo)
df_ordenado
  • Taking advantage of the question, I need to pick up a column in the middle of the DF and play to the end, as I do ?

2

Using everything() to indicate which other columns you do not want to change. It is more practical for many-column dataframes.

library(dplyr)

df_ordenado <- df %>% select(Indice, Produto, everything())
df_ordenado

1

Using the same names as the @Marcusnunes reply, you can also do it with the base R:

df_ordenado <- df[c("Indice", "Produto", "Classificação", "Comun", "Quilo")]

0

One solution is to use the function relocate, of dplyr, built for this purpose:

library(dplyr)

df %>%
  relocate(Indice)

Your advantage over dplyr::select is not needing to write all variables. The arguments .before and .after further increase the flexibility of the function relocate.

For more details about this function, click here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.