Skip to contents

This function rotates a data frame, i.e. columns become rows and vice versa. It's the equivalent of using t() but restores the data.frame class, preserves attributes and prints a warning if the data type is modified (see example).

Usage

data_rotate(data, rownames = NULL, colnames = FALSE, verbose = TRUE)

data_transpose(data, rownames = NULL, colnames = FALSE, verbose = TRUE)

Arguments

data

A data frame.

rownames

Character vector (optional). If not NULL, the data frame's rownames will be added as (first) column to the output, with rownames being the name of this column.

colnames

Logical or character vector (optional). If TRUE, the values of the first column in x will be used as column names in the rotated data frame. If a character vector, values from that column are used as column names.

verbose

Toggle warnings.

Value

A (rotated) data frame.

See also

Examples

x <- mtcars[1:3, 1:4]

x
#>                mpg cyl disp  hp
#> Mazda RX4     21.0   6  160 110
#> Mazda RX4 Wag 21.0   6  160 110
#> Datsun 710    22.8   4  108  93

data_rotate(x)
#>      Mazda RX4 Mazda RX4 Wag Datsun 710
#> mpg         21            21       22.8
#> cyl          6             6        4.0
#> disp       160           160      108.0
#> hp         110           110       93.0
data_rotate(x, rownames = "property")
#>   property Mazda RX4 Mazda RX4 Wag Datsun 710
#> 1      mpg        21            21       22.8
#> 2      cyl         6             6        4.0
#> 3     disp       160           160      108.0
#> 4       hp       110           110       93.0

# use values in 1. column as column name
data_rotate(x, colnames = TRUE)
#>       21  21 22.8
#> cyl    6   6    4
#> disp 160 160  108
#> hp   110 110   93
data_rotate(x, rownames = "property", colnames = TRUE)
#>   property  21  21 22.8
#> 1      cyl   6   6    4
#> 2     disp 160 160  108
#> 3       hp 110 110   93

# warn that data types are changed
str(data_rotate(iris[1:4, ]))
#> Warning: Your data frame contains mixed types of data. After transposition, all
#>   variables will be transformed into characters.
#> 'data.frame':	5 obs. of  4 variables:
#>  $ 1: chr  "5.1" "3.5" "1.4" "0.2" ...
#>  $ 2: chr  "4.9" "3.0" "1.4" "0.2" ...
#>  $ 3: chr  "4.7" "3.2" "1.3" "0.2" ...
#>  $ 4: chr  "4.6" "3.1" "1.5" "0.2" ...

# use either first column or specific column for column names
x <- data.frame(a = 1:5, b = 11:15, c = letters[1:5], d = rnorm(5))
data_rotate(x, colnames = TRUE)
#> Warning: Your data frame contains mixed types of data. After transposition, all
#>   variables will be transformed into characters.
#>            1          2          3          4          5
#> b         11         12         13         14         15
#> c          a          b          c          d          e
#> d -0.6868529 -0.4456620  1.2240818  0.3598138  0.4007715
data_rotate(x, colnames = "c")
#> Warning: Your data frame contains mixed types of data. After transposition, all
#>   variables will be transformed into characters.
#>            a         b         c          d          e
#> a  1.0000000  2.000000  3.000000  4.0000000  5.0000000
#> b 11.0000000 12.000000 13.000000 14.0000000 15.0000000
#> d -0.6868529 -0.445662  1.224082  0.3598138  0.4007715