How to change order of appearance of columns in a Data Frame?

4

I have 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, how can I do that?

I want it to look like this:

   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     
    
asked by anonymous 09.03.2017 / 19:30

3 answers

2

Assuming the data frame is called df :

library(dplyr)
df_ordenado <- df %>%
  select(Indice, Produto, Classificação, Comun, Quilo)
df_ordenado
    
09.03.2017 / 21:43
1

Using the same @MarcusNunes response names, you can also do with base R:

df_ordenado <- df[c("Indice", "Produto", "Classificação", "Comun", "Quilo")]
    
10.03.2017 / 23:46
1

Using everything to indicate the other columns you do not want to change. It's more practical for dataframes with many columns.

library (dplyr) df_confirmed % select (Index, Product, everything ()) df_order

    
20.03.2017 / 01:00