How to Transpose a Data Frame in R (2024)

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 datadata("mtcars")# transposet_mtcars <- transpose(mtcars)# get row and colnames in ordercolnames(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.

Table of contents

  • Get Transpose of a Data Frame Using the t() function
  • Get Transpose of a Data Frame Using data.table
  • Get Transpose of a Data Frame Using tidyverse
  • Summary

Get Transpose of a Data Frame Using the t() function

Consider the following data frame:

# Define data framedf <- 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 framerow.names(df) <- c('A', 'B', 'C', 'D', 'E', 'F')# Print data frameprint(df)
 x y zA 1 2 5B 3 4 10C 5 6 15D 7 8 20E 9 10 25F 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 framedf_t <- t(df)print(df_t)
 A B C D E Fx 1 3 5 7 9 11y 2 4 6 8 10 12z 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 framehead(mtcars)
 mpg cyl disp hp drat wt qsec vs am gear carbMazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2Valiant 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 namescolnames(t_mtcars) <- rownames(mtcars)rownames(t_mtcars) <- colnames(mtcars)# Print first 5 columns of data framehead(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 Sportaboutmpg 21.00 21.000 22.80 21.400 18.70cyl 6.00 6.000 4.00 6.000 8.00disp 160.00 160.000 108.00 258.000 360.00hp 110.00 110.000 93.00 110.000 175.00drat 3.90 3.900 3.85 3.080 3.15wt 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 framehead(mtcars)
 mpg cyl disp hp drat wt qsec vs am gear carbMazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2Valiant 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 710mpg am 0.000 0.00 0.00 0.000 1.00cyl carb 2.000 4.00 4.00 4.000 1.00disp cyl 8.000 8.00 8.00 8.000 4.00hp disp 304.000 472.00 350.00 440.000 108.00drat drat 3.150 2.93 3.73 3.230 3.85wt gear 3.000 3.00 3.00 3.000 4.00qsec hp 150.000 205.00 245.00 230.000 93.00vs mpg 15.200 10.40 13.30 14.700 22.80am qsec 17.300 17.98 15.41 17.420 18.61gear vs 0.000 0.00 0.00 0.000 1.00carb 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 theonline courses page on Rto learn more about coding in R for data science and machine learning.

Have fun and happy researching!

Related

How to Transpose a Data Frame in R (2024)
Top Articles
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 6366

Rating: 4.4 / 5 (65 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.