An efficient way to transpose data frames(data.frame/data.table/tibble).
t_dt(.data)
A transposed data.frame
This function would return the original data.frame structure, keeping all the row names and column names. If the row names are not available or, "V1,V2..." will be provided.
t_dt(iris)
#> V1 V2 V3 V4 V5 V6 V7 V8 V9
#> Sepal.Length 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4
#> Sepal.Width 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9
#> Petal.Length 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4
#> Petal.Width 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2
#> Species setosa setosa setosa setosa setosa setosa setosa setosa setosa
#> V10 V11 V12 V13 V14 V15 V16 V17 V18
#> Sepal.Length 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1
#> Sepal.Width 3.1 3.7 3.4 3 3 4 4.4 3.9 3.5
#> Petal.Length 1.5 1.5 1.6 1.4 1.1 1.2 1.5 1.3 1.4
#> Petal.Width 0.1 0.2 0.2 0.1 0.1 0.2 0.4 0.4 0.3
#> Species setosa setosa setosa setosa setosa setosa setosa setosa setosa
#> V19 V20 V21 V22 V23 V24 V25 V26 V27
#> Sepal.Length 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5 5
#> Sepal.Width 3.8 3.8 3.4 3.7 3.6 3.3 3.4 3 3.4
#> Petal.Length 1.7 1.5 1.7 1.5 1 1.7 1.9 1.6 1.6
#> Petal.Width 0.3 0.3 0.2 0.4 0.2 0.5 0.2 0.2 0.4
#> Species setosa setosa setosa setosa setosa setosa setosa setosa setosa
#> V28 V29 V30 V31 V32 V33 V34 V35 V36
#> Sepal.Length 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5
#> Sepal.Width 3.5 3.4 3.2 3.1 3.4 4.1 4.2 3.1 3.2
#> Petal.Length 1.5 1.4 1.6 1.6 1.5 1.5 1.4 1.5 1.2
#> Petal.Width 0.2 0.2 0.2 0.2 0.4 0.1 0.2 0.2 0.2
#> Species setosa setosa setosa setosa setosa setosa setosa setosa setosa
#> V37 V38 V39 V40 V41 V42 V43 V44 V45
#> Sepal.Length 5.5 4.9 4.4 5.1 5 4.5 4.4 5 5.1
#> Sepal.Width 3.5 3.6 3 3.4 3.5 2.3 3.2 3.5 3.8
#> Petal.Length 1.3 1.4 1.3 1.5 1.3 1.3 1.3 1.6 1.9
#> Petal.Width 0.2 0.1 0.2 0.2 0.3 0.3 0.2 0.6 0.4
#> Species setosa setosa setosa setosa setosa setosa setosa setosa setosa
#> V46 V47 V48 V49 V50 V51 V52
#> Sepal.Length 4.8 5.1 4.6 5.3 5 7 6.4
#> Sepal.Width 3 3.8 3.2 3.7 3.3 3.2 3.2
#> Petal.Length 1.4 1.6 1.4 1.5 1.4 4.7 4.5
#> Petal.Width 0.3 0.2 0.2 0.2 0.2 1.4 1.5
#> Species setosa setosa setosa setosa setosa versicolor versicolor
#> V53 V54 V55 V56 V57 V58
#> Sepal.Length 6.9 5.5 6.5 5.7 6.3 4.9
#> Sepal.Width 3.1 2.3 2.8 2.8 3.3 2.4
#> Petal.Length 4.9 4 4.6 4.5 4.7 3.3
#> Petal.Width 1.5 1.3 1.5 1.3 1.6 1
#> Species versicolor versicolor versicolor versicolor versicolor versicolor
#> V59 V60 V61 V62 V63 V64
#> Sepal.Length 6.6 5.2 5 5.9 6 6.1
#> Sepal.Width 2.9 2.7 2 3 2.2 2.9
#> Petal.Length 4.6 3.9 3.5 4.2 4 4.7
#> Petal.Width 1.3 1.4 1 1.5 1 1.4
#> Species versicolor versicolor versicolor versicolor versicolor versicolor
#> V65 V66 V67 V68 V69 V70
#> Sepal.Length 5.6 6.7 5.6 5.8 6.2 5.6
#> Sepal.Width 2.9 3.1 3 2.7 2.2 2.5
#> Petal.Length 3.6 4.4 4.5 4.1 4.5 3.9
#> Petal.Width 1.3 1.4 1.5 1 1.5 1.1
#> Species versicolor versicolor versicolor versicolor versicolor versicolor
#> V71 V72 V73 V74 V75 V76
#> Sepal.Length 5.9 6.1 6.3 6.1 6.4 6.6
#> Sepal.Width 3.2 2.8 2.5 2.8 2.9 3
#> Petal.Length 4.8 4 4.9 4.7 4.3 4.4
#> Petal.Width 1.8 1.3 1.5 1.2 1.3 1.4
#> Species versicolor versicolor versicolor versicolor versicolor versicolor
#> V77 V78 V79 V80 V81 V82
#> Sepal.Length 6.8 6.7 6 5.7 5.5 5.5
#> Sepal.Width 2.8 3 2.9 2.6 2.4 2.4
#> Petal.Length 4.8 5 4.5 3.5 3.8 3.7
#> Petal.Width 1.4 1.7 1.5 1 1.1 1
#> Species versicolor versicolor versicolor versicolor versicolor versicolor
#> V83 V84 V85 V86 V87 V88
#> Sepal.Length 5.8 6 5.4 6 6.7 6.3
#> Sepal.Width 2.7 2.7 3 3.4 3.1 2.3
#> Petal.Length 3.9 5.1 4.5 4.5 4.7 4.4
#> Petal.Width 1.2 1.6 1.5 1.6 1.5 1.3
#> Species versicolor versicolor versicolor versicolor versicolor versicolor
#> V89 V90 V91 V92 V93 V94
#> Sepal.Length 5.6 5.5 5.5 6.1 5.8 5
#> Sepal.Width 3 2.5 2.6 3 2.6 2.3
#> Petal.Length 4.1 4 4.4 4.6 4 3.3
#> Petal.Width 1.3 1.3 1.2 1.4 1.2 1
#> Species versicolor versicolor versicolor versicolor versicolor versicolor
#> V95 V96 V97 V98 V99 V100
#> Sepal.Length 5.6 5.7 5.7 6.2 5.1 5.7
#> Sepal.Width 2.7 3 2.9 2.9 2.5 2.8
#> Petal.Length 4.2 4.2 4.2 4.3 3 4.1
#> Petal.Width 1.3 1.2 1.3 1.3 1.1 1.3
#> Species versicolor versicolor versicolor versicolor versicolor versicolor
#> V101 V102 V103 V104 V105 V106
#> Sepal.Length 6.3 5.8 7.1 6.3 6.5 7.6
#> Sepal.Width 3.3 2.7 3 2.9 3 3
#> Petal.Length 6 5.1 5.9 5.6 5.8 6.6
#> Petal.Width 2.5 1.9 2.1 1.8 2.2 2.1
#> Species virginica virginica virginica virginica virginica virginica
#> V107 V108 V109 V110 V111 V112
#> Sepal.Length 4.9 7.3 6.7 7.2 6.5 6.4
#> Sepal.Width 2.5 2.9 2.5 3.6 3.2 2.7
#> Petal.Length 4.5 6.3 5.8 6.1 5.1 5.3
#> Petal.Width 1.7 1.8 1.8 2.5 2 1.9
#> Species virginica virginica virginica virginica virginica virginica
#> V113 V114 V115 V116 V117 V118
#> Sepal.Length 6.8 5.7 5.8 6.4 6.5 7.7
#> Sepal.Width 3 2.5 2.8 3.2 3 3.8
#> Petal.Length 5.5 5 5.1 5.3 5.5 6.7
#> Petal.Width 2.1 2 2.4 2.3 1.8 2.2
#> Species virginica virginica virginica virginica virginica virginica
#> V119 V120 V121 V122 V123 V124
#> Sepal.Length 7.7 6 6.9 5.6 7.7 6.3
#> Sepal.Width 2.6 2.2 3.2 2.8 2.8 2.7
#> Petal.Length 6.9 5 5.7 4.9 6.7 4.9
#> Petal.Width 2.3 1.5 2.3 2 2 1.8
#> Species virginica virginica virginica virginica virginica virginica
#> V125 V126 V127 V128 V129 V130
#> Sepal.Length 6.7 7.2 6.2 6.1 6.4 7.2
#> Sepal.Width 3.3 3.2 2.8 3 2.8 3
#> Petal.Length 5.7 6 4.8 4.9 5.6 5.8
#> Petal.Width 2.1 1.8 1.8 1.8 2.1 1.6
#> Species virginica virginica virginica virginica virginica virginica
#> V131 V132 V133 V134 V135 V136
#> Sepal.Length 7.4 7.9 6.4 6.3 6.1 7.7
#> Sepal.Width 2.8 3.8 2.8 2.8 2.6 3
#> Petal.Length 6.1 6.4 5.6 5.1 5.6 6.1
#> Petal.Width 1.9 2 2.2 1.5 1.4 2.3
#> Species virginica virginica virginica virginica virginica virginica
#> V137 V138 V139 V140 V141 V142
#> Sepal.Length 6.3 6.4 6 6.9 6.7 6.9
#> Sepal.Width 3.4 3.1 3 3.1 3.1 3.1
#> Petal.Length 5.6 5.5 4.8 5.4 5.6 5.1
#> Petal.Width 2.4 1.8 1.8 2.1 2.4 2.3
#> Species virginica virginica virginica virginica virginica virginica
#> V143 V144 V145 V146 V147 V148
#> Sepal.Length 5.8 6.8 6.7 6.7 6.3 6.5
#> Sepal.Width 2.7 3.2 3.3 3 2.5 3
#> Petal.Length 5.1 5.9 5.7 5.2 5 5.2
#> Petal.Width 1.9 2.3 2.5 2.3 1.9 2
#> Species virginica virginica virginica virginica virginica virginica
#> V149 V150
#> Sepal.Length 6.2 5.9
#> Sepal.Width 3.4 3
#> Petal.Length 5.4 5.1
#> Petal.Width 2.3 1.8
#> Species virginica virginica
t_dt(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