Skip to contents

This function will invert the order of columns in a data frame and optionally returns the data frame as a tibble or data table.

Usage

invert_col(df, out)

Arguments

df

Short for data frame, the first argument should be a data frame of any type.

out

Short for output, this argument determines the form of the resulting data. Options include 'DF' for data frame, 'TB' for tibble, and 'DT' for data table.

Value

Either a data frame, data table, or tibble with column order inverted.

Details

Function will return errors if first argument is not of type data frame. out is an optional argument that determines if the output is a data frame, tibble, or data table. Defaults to data frame if left empty.

Examples

invert_col(mtcars)
#>                     carb gear am vs  qsec    wt drat  hp  disp cyl  mpg
#> Mazda RX4              4    4  1  0 16.46 2.620 3.90 110 160.0   6 21.0
#> Mazda RX4 Wag          4    4  1  0 17.02 2.875 3.90 110 160.0   6 21.0
#> Datsun 710             1    4  1  1 18.61 2.320 3.85  93 108.0   4 22.8
#> Hornet 4 Drive         1    3  0  1 19.44 3.215 3.08 110 258.0   6 21.4
#> Hornet Sportabout      2    3  0  0 17.02 3.440 3.15 175 360.0   8 18.7
#> Valiant                1    3  0  1 20.22 3.460 2.76 105 225.0   6 18.1
#> Duster 360             4    3  0  0 15.84 3.570 3.21 245 360.0   8 14.3
#> Merc 240D              2    4  0  1 20.00 3.190 3.69  62 146.7   4 24.4
#> Merc 230               2    4  0  1 22.90 3.150 3.92  95 140.8   4 22.8
#> Merc 280               4    4  0  1 18.30 3.440 3.92 123 167.6   6 19.2
#> Merc 280C              4    4  0  1 18.90 3.440 3.92 123 167.6   6 17.8
#> Merc 450SE             3    3  0  0 17.40 4.070 3.07 180 275.8   8 16.4
#> Merc 450SL             3    3  0  0 17.60 3.730 3.07 180 275.8   8 17.3
#> Merc 450SLC            3    3  0  0 18.00 3.780 3.07 180 275.8   8 15.2
#> Cadillac Fleetwood     4    3  0  0 17.98 5.250 2.93 205 472.0   8 10.4
#> Lincoln Continental    4    3  0  0 17.82 5.424 3.00 215 460.0   8 10.4
#> Chrysler Imperial      4    3  0  0 17.42 5.345 3.23 230 440.0   8 14.7
#> Fiat 128               1    4  1  1 19.47 2.200 4.08  66  78.7   4 32.4
#> Honda Civic            2    4  1  1 18.52 1.615 4.93  52  75.7   4 30.4
#> Toyota Corolla         1    4  1  1 19.90 1.835 4.22  65  71.1   4 33.9
#> Toyota Corona          1    3  0  1 20.01 2.465 3.70  97 120.1   4 21.5
#> Dodge Challenger       2    3  0  0 16.87 3.520 2.76 150 318.0   8 15.5
#> AMC Javelin            2    3  0  0 17.30 3.435 3.15 150 304.0   8 15.2
#> Camaro Z28             4    3  0  0 15.41 3.840 3.73 245 350.0   8 13.3
#> Pontiac Firebird       2    3  0  0 17.05 3.845 3.08 175 400.0   8 19.2
#> Fiat X1-9              1    4  1  1 18.90 1.935 4.08  66  79.0   4 27.3
#> Porsche 914-2          2    5  1  0 16.70 2.140 4.43  91 120.3   4 26.0
#> Lotus Europa           2    5  1  1 16.90 1.513 3.77 113  95.1   4 30.4
#> Ford Pantera L         4    5  1  0 14.50 3.170 4.22 264 351.0   8 15.8
#> Ferrari Dino           6    5  1  0 15.50 2.770 3.62 175 145.0   6 19.7
#> Maserati Bora          8    5  1  0 14.60 3.570 3.54 335 301.0   8 15.0
#> Volvo 142E             2    4  1  1 18.60 2.780 4.11 109 121.0   4 21.4
invert_col(mtcars, "TB")
#> # A tibble: 32 x 11
#>     carb  gear    am    vs  qsec    wt  drat    hp  disp   cyl   mpg
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1     4     4     1     0  16.5  2.62  3.9    110  160      6  21  
#>  2     4     4     1     0  17.0  2.88  3.9    110  160      6  21  
#>  3     1     4     1     1  18.6  2.32  3.85    93  108      4  22.8
#>  4     1     3     0     1  19.4  3.22  3.08   110  258      6  21.4
#>  5     2     3     0     0  17.0  3.44  3.15   175  360      8  18.7
#>  6     1     3     0     1  20.2  3.46  2.76   105  225      6  18.1
#>  7     4     3     0     0  15.8  3.57  3.21   245  360      8  14.3
#>  8     2     4     0     1  20    3.19  3.69    62  147.     4  24.4
#>  9     2     4     0     1  22.9  3.15  3.92    95  141.     4  22.8
#> 10     4     4     0     1  18.3  3.44  3.92   123  168.     6  19.2
#> # ... with 22 more rows