Skip to contents

This function flips rows and columns in a data frame and optionally returns the flipped data frame as a tibble or data table.

Usage

flip(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 columns and rows flipped.

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

flip(mtcars)
#>      Mazda.RX4 Mazda.RX4.Wag Datsun.710 Hornet.4.Drive Hornet.Sportabout
#> mpg      21.00        21.000      22.80         21.400             18.70
#> cyl       6.00         6.000       4.00          6.000              8.00
#> disp    160.00       160.000     108.00        258.000            360.00
#> hp      110.00       110.000      93.00        110.000            175.00
#> drat      3.90         3.900       3.85          3.080              3.15
#> wt        2.62         2.875       2.32          3.215              3.44
#> qsec     16.46        17.020      18.61         19.440             17.02
#> vs        0.00         0.000       1.00          1.000              0.00
#> am        1.00         1.000       1.00          0.000              0.00
#> gear      4.00         4.000       4.00          3.000              3.00
#> carb      4.00         4.000       1.00          1.000              2.00
#>      Valiant Duster.360 Merc.240D Merc.230 Merc.280 Merc.280C Merc.450SE
#> mpg    18.10      14.30     24.40    22.80    19.20     17.80      16.40
#> cyl     6.00       8.00      4.00     4.00     6.00      6.00       8.00
#> disp  225.00     360.00    146.70   140.80   167.60    167.60     275.80
#> hp    105.00     245.00     62.00    95.00   123.00    123.00     180.00
#> drat    2.76       3.21      3.69     3.92     3.92      3.92       3.07
#> wt      3.46       3.57      3.19     3.15     3.44      3.44       4.07
#> qsec   20.22      15.84     20.00    22.90    18.30     18.90      17.40
#> vs      1.00       0.00      1.00     1.00     1.00      1.00       0.00
#> am      0.00       0.00      0.00     0.00     0.00      0.00       0.00
#> gear    3.00       3.00      4.00     4.00     4.00      4.00       3.00
#> carb    1.00       4.00      2.00     2.00     4.00      4.00       3.00
#>      Merc.450SL Merc.450SLC Cadillac.Fleetwood Lincoln.Continental
#> mpg       17.30       15.20              10.40              10.400
#> cyl        8.00        8.00               8.00               8.000
#> disp     275.80      275.80             472.00             460.000
#> hp       180.00      180.00             205.00             215.000
#> drat       3.07        3.07               2.93               3.000
#> wt         3.73        3.78               5.25               5.424
#> qsec      17.60       18.00              17.98              17.820
#> vs         0.00        0.00               0.00               0.000
#> am         0.00        0.00               0.00               0.000
#> gear       3.00        3.00               3.00               3.000
#> carb       3.00        3.00               4.00               4.000
#>      Chrysler.Imperial Fiat.128 Honda.Civic Toyota.Corolla Toyota.Corona
#> mpg             14.700    32.40      30.400         33.900        21.500
#> cyl              8.000     4.00       4.000          4.000         4.000
#> disp           440.000    78.70      75.700         71.100       120.100
#> hp             230.000    66.00      52.000         65.000        97.000
#> drat             3.230     4.08       4.930          4.220         3.700
#> wt               5.345     2.20       1.615          1.835         2.465
#> qsec            17.420    19.47      18.520         19.900        20.010
#> vs               0.000     1.00       1.000          1.000         1.000
#> am               0.000     1.00       1.000          1.000         0.000
#> gear             3.000     4.00       4.000          4.000         3.000
#> carb             4.000     1.00       2.000          1.000         1.000
#>      Dodge.Challenger AMC.Javelin Camaro.Z28 Pontiac.Firebird Fiat.X1.9
#> mpg             15.50      15.200      13.30           19.200    27.300
#> cyl              8.00       8.000       8.00            8.000     4.000
#> disp           318.00     304.000     350.00          400.000    79.000
#> hp             150.00     150.000     245.00          175.000    66.000
#> drat             2.76       3.150       3.73            3.080     4.080
#> wt               3.52       3.435       3.84            3.845     1.935
#> qsec            16.87      17.300      15.41           17.050    18.900
#> vs               0.00       0.000       0.00            0.000     1.000
#> am               0.00       0.000       0.00            0.000     1.000
#> gear             3.00       3.000       3.00            3.000     4.000
#> carb             2.00       2.000       4.00            2.000     1.000
#>      Porsche.914.2 Lotus.Europa Ford.Pantera.L Ferrari.Dino Maserati.Bora
#> mpg          26.00       30.400          15.80        19.70         15.00
#> cyl           4.00        4.000           8.00         6.00          8.00
#> disp        120.30       95.100         351.00       145.00        301.00
#> hp           91.00      113.000         264.00       175.00        335.00
#> drat          4.43        3.770           4.22         3.62          3.54
#> wt            2.14        1.513           3.17         2.77          3.57
#> qsec         16.70       16.900          14.50        15.50         14.60
#> vs            0.00        1.000           0.00         0.00          0.00
#> am            1.00        1.000           1.00         1.00          1.00
#> gear          5.00        5.000           5.00         5.00          5.00
#> carb          2.00        2.000           4.00         6.00          8.00
#>      Volvo.142E
#> mpg       21.40
#> cyl        4.00
#> disp     121.00
#> hp       109.00
#> drat       4.11
#> wt         2.78
#> qsec      18.60
#> vs         1.00
#> am         1.00
#> gear       4.00
#> carb       2.00
flip(mtcars, "TB")
#> # A tibble: 11 x 32
#>    Mazda.RX4 Mazda.RX4.Wag Datsun.710 Hornet.4.Drive Hornet.Sportabout Valiant
#>        <dbl>         <dbl>      <dbl>          <dbl>             <dbl>   <dbl>
#>  1     21            21         22.8           21.4              18.7    18.1 
#>  2      6             6          4              6                 8       6   
#>  3    160           160        108            258               360     225   
#>  4    110           110         93            110               175     105   
#>  5      3.9           3.9        3.85           3.08              3.15    2.76
#>  6      2.62          2.88       2.32           3.22              3.44    3.46
#>  7     16.5          17.0       18.6           19.4              17.0    20.2 
#>  8      0             0          1              1                 0       1   
#>  9      1             1          1              0                 0       0   
#> 10      4             4          4              3                 3       3   
#> 11      4             4          1              1                 2       1   
#> # ... with 26 more variables: Duster.360 <dbl>, Merc.240D <dbl>,
#> #   Merc.230 <dbl>, Merc.280 <dbl>, Merc.280C <dbl>, Merc.450SE <dbl>,
#> #   Merc.450SL <dbl>, Merc.450SLC <dbl>, Cadillac.Fleetwood <dbl>,
#> #   Lincoln.Continental <dbl>, Chrysler.Imperial <dbl>, Fiat.128 <dbl>,
#> #   Honda.Civic <dbl>, Toyota.Corolla <dbl>, Toyota.Corona <dbl>,
#> #   Dodge.Challenger <dbl>, AMC.Javelin <dbl>, Camaro.Z28 <dbl>,
#> #   Pontiac.Firebird <dbl>, Fiat.X1.9 <dbl>, Porsche.914.2 <dbl>, ...