How do I transpose a data frame in R? Can you provide examples?

How do I transpose a data frame in R? Can you provide examples?

In R, transposing a data frame involves switching the rows and columns of a dataset. This can be done using the “t()” function or the “transpose()” function. Examples of transposing a data frame can be found in various R packages or by using built-in datasets.

Transpose a Data Frame in R (With Examples)


There are two common methods you can use to transpose a data frame in R:

Method 1: Use Base R

#transpose data frame
t(df)

Method 2: Use data.table

library(data.table)

#transpose data frame
df_t <- transpose(df)

#redefine row and column names
rownames(df_t) <- colnames(df)
colnames(df_t) <- rownames(df)

The following examples show how to use each of these methods in practice.

Method 1: Transpose Data Frame Using Base R

Suppose we have the following data frame:

#create data frame
df <- data.frame(A = c(1, 2, 3, 4, 5),
                 B = c(6, 7, 8, 9, 10),
                 C = c(11, 12, 13, 14, 15))

#define row names
row.names(df) <- c('One', 'Two', 'Three', 'Four', 'Five')

#view data frame
df

      A  B  C
One   1  6 11
Two   2  7 12
Three 3  8 13
Four  4  9 14
Five  5 10 15

We can use the t() function from base R to quickly transpose the data frame:

#transpose data frame
t(df)

  One Two Three Four Five
A   1   2     3    4    5
B   6   7     8    9   10
C  11  12    13   14   15

The rows and the columns are now switched.

Method 2: Transpose Data Frame Using data.table

Once again suppose we have the following data frame:

#create data frame
df <- data.frame(A = c(1, 2, 3, 4, 5),
                 B = c(6, 7, 8, 9, 10),
                 C = c(11, 12, 13, 14, 15))

#define row names
row.names(df) <- c('One', 'Two', 'Three', 'Four', 'Five')

#view data frame
df

      A  B  C
One   1  6 11
Two   2  7 12
Three 3  8 13
Four  4  9 14
Five  5 10 15

We can use the transpose() function from the data.table package to quickly transpose the data frame:

library(data.table)

#transpose data frame
df_t <- transpose(df)

#redefine row and column names
rownames(df_t) <- colnames(df)
colnames(df_t) <- rownames(df)

#display transposed data frame
df_t

  One Two Three Four Five
A   1   2     3    4    5
B   6   7     8    9   10
C  11  12    13   14   15

Note: The data.table method will be much faster than base R if you are working with an extremely large data frame.

The following tutorials explain how to perform other common operations on data frames in R:

Cite this article

stats writer (2024). How do I transpose a data frame in R? Can you provide examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-transpose-a-data-frame-in-r-can-you-provide-examples/

stats writer. "How do I transpose a data frame in R? Can you provide examples?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-do-i-transpose-a-data-frame-in-r-can-you-provide-examples/.

stats writer. "How do I transpose a data frame in R? Can you provide examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-transpose-a-data-frame-in-r-can-you-provide-examples/.

stats writer (2024) 'How do I transpose a data frame in R? Can you provide examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-transpose-a-data-frame-in-r-can-you-provide-examples/.

[1] stats writer, "How do I transpose a data frame in R? Can you provide examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How do I transpose a data frame in R? Can you provide examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top