flip.Rd
This function takes a data frame or matrix and flips its rows and columns, effectively transposing the data. Optionally, it can include row names in the resulting data frame.
flip(x, rownames = NULL)
A data frame with flipped rows and columns. If rownames
is
TRUE
, row names (if x
is a matrix) or the first column will
contain the original row names (if x
is a data frame).
t
for transposing matrices and data frames.
# Create a sample data frame
df <- data.frame(
Car = c("BMW", "Volvo", "Honda"),
Cost = (1:3)*10000,
Speed = c(140, 160, 180),
Colour = c("Red", "Blue", "Brown")
)
# Flip the data frame
flip(df)
#> Car BMW Volvo Honda
#> 1 Cost 10000 20000 30000
#> 2 Speed 140 160 180
#> 3 Colour Red Blue Brown
flip(df) |> nice_table()
BMW
Volvo
Honda
# Flip the data frame with row names included
flip(df, rownames = TRUE)
#> 1 2 3
#> 1 Car BMW Volvo Honda
#> 2 Cost 10000 20000 30000
#> 3 Speed 140 160 180
#> 4 Colour Red Blue Brown