Skip to contents

This function transposes a data.frame by switching its rows and columns.

Usage

transpose_df(df, names_in_first_col = TRUE)

Arguments

df

data.frame

names_in_first_col

(logical) whether to use first column as new column names (see Details; default TRUE)

Value

(data.frame) df transposed

Details

If names_in_first_col is set to FALSE, then the first column in the transposed data.frame will be named Columns, indicating original column names, and subsequent columns (original rows) will be named with integers starting at "1".

Examples

df |>
   head() |>
   transpose_df(FALSE)
#>   Columns         1           2         3           4          5          6
#> 2      x1 0.5013220  0.67713213 0.3655292  0.06289236 -0.7965901 -0.3172710
#> 3      x2 0.9954618 -0.03467442 0.3071701 -0.34876756 -1.5676613 -0.3285483
#> 4      x3 0.7820043 -0.96262616 0.4229571 -0.58852354 -2.0316797 -0.8842447
#> 5      y1 0.7579626  1.00556393 0.2549780 -0.08954770 -1.3800225 -0.3526999
#> 6      y2 0.8785043 -0.58446702 0.7607614 -0.29437896 -1.7986579 -1.0720987
#> 7      y3 0.4281959 -0.87923290 0.4599168 -0.72405280 -1.9365447 -1.2035635