How to transpose two-dimensional data in R?

We can transpose a data frame or a matrix in R so that the columns become the rows and vice versa. For this purpose, we need to use the t() function of the base R. For example:

df <- data.frame(col_1=c(10, 20, 30), col_2=c(11, 22, 33))
print(df)
​
transposed_df <- t(df)
print(transposed_df)Powered By 

Output:

 col_1 col_2
1    10    11
2    20    22
3    30    33
      [,1] [,2] [,3]
col_1   10   20   30
col_2   11   22   33

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *