The easiest way to transpose a data frame is to use the transpose()
function from the data.table
library. For example,
library(data.table) # get data data("mtcars") # transpose t_mtcars <- transpose(mtcars) # get row and colnames in order colnames(t_mtcars) <- rownames(mtcars) rownames(t_mtcars) <- colnames(mtcars)
Ensure that you set the row and column names to match the transposed rows and columns.
This tutorial will go through two ways of transposing a data frame in R with code examples.
Get Transpose of a Data Frame Using the t() function
Consider the following data frame:
# Define data frame df <- data.frame(x = c(1, 3, 5, 7, 9, 11), y=c(2, 4, 6, 8, 10, 12), z=c(5, 10, 15, 20, 25, 30)) # Set row names of data frame row.names(df) <- c('A', 'B', 'C', 'D', 'E', 'F') # Print data frame print(df)
x y z A 1 2 5 B 3 4 10 C 5 6 15 D 7 8 20 E 9 10 25 F 11 12 30
We can use the base R t() function to transpose the data frame. When using the t()
function the row and column names are switched automatically.
# Transpose of data frame df_t <- t(df) print(df_t)
A B C D E F x 1 3 5 7 9 11 y 2 4 6 8 10 12 z 5 10 15 20 25 30
Get Transpose of a Data Frame Using data.table
Consider the mtcars dataset:
# Get first 6 rows of data frame head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
We can use the transpose()
function from the data.table
package:
Note, that we need to set the row and column names to match the transposed data frame.
# Transpose data frame t_mtcars <-transpose(mtcars) # Redefine row and column names colnames(t_mtcars) <- rownames(mtcars) rownames(t_mtcars) <- colnames(mtcars) # Print first 5 columns of data frame head(t_mtcars[, 1:5])
Let’s run the code to see the transposed data frame.
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
We can see that the columns are the automobile names and the rows are the aspects of automobile design.
Get Transpose of a Data Frame Using tidyverse
We can also get the transpose of a data frame using the tidyverse package. First, consider the mtcars dataset:
# Get first 6 rows of data frame head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
We can use the rownames_to_column
function with gather
and spread
to transpose the data frame as follows:
install.packages("tidyverse") library(tidyverse) t_mtcars<- mtcars %>% rownames_to_column %>% gather(var, value, -rowname) %>% spread(rowname, value) rownames(t_mtcars) <- colnames(mtcars) print(t_mtcars)
Let’s run the code to get the transposed data frame:
var AMC Javelin Cadillac Fleetwood Camaro Z28 Chrysler Imperial Datsun 710 mpg am 0.000 0.00 0.00 0.000 1.00 cyl carb 2.000 4.00 4.00 4.000 1.00 disp cyl 8.000 8.00 8.00 8.000 4.00 hp disp 304.000 472.00 350.00 440.000 108.00 drat drat 3.150 2.93 3.73 3.230 3.85 wt gear 3.000 3.00 3.00 3.000 4.00 qsec hp 150.000 205.00 245.00 230.000 93.00 vs mpg 15.200 10.40 13.30 14.700 22.80 am qsec 17.300 17.98 15.41 17.420 18.61 gear vs 0.000 0.00 0.00 0.000 1.00 carb wt 3.435 5.25 3.84 5.345 2.32
We can also use the pivot_longer
and pivot_wider
as follows:
mtcars %>% tibble::rownames_to_column() %>% pivot_longer(-rowname) %>% pivot_wider(names_from=rowname, values_from=value)
Let’s run the code to get the transposed data frame.
# A tibble: 11 × 33 name `Mazda RX4` `Mazda RX4 Wag` `Datsun 710` `Hornet 4 Drive` `Hornet Sportabout` <chr> <dbl> <dbl> <dbl> <dbl> <dbl> 1 mpg 21 21 22.8 21.4 18.7 2 cyl 6 6 4 6 8 3 disp 160 160 108 258 360 4 hp 110 110 93 110 175 5 drat 3.9 3.9 3.85 3.08 3.15 6 wt 2.62 2.88 2.32 3.22 3.44 7 qsec 16.5 17.0 18.6 19.4 17.0 8 vs 0 0 1 1 0 9 am 1 1 1 0 0 10 gear 4 4 4 3 3 11 carb 4 4 1 1 2 # … with 27 more variables: Valiant <dbl>, `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>, …
Summary
Congratulations on reading to the end of this tutorial!
For further reading on R, go to the articles:
- How to Solve R Error as.Date.numeric(x) : ‘origin’ must be supplied
- How to Solve R Error: invalid (do_set) left-hand side to assignment
- How to Solve R Error: plot.new has not been called yet
Go to the online courses page on R to learn more about coding in R for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.