0

I have a long data frame with multiple rows. I wish to reshape it to wide, with only 1 row left.

dput(df) > 
structure(list(A = c(2, 4, 6), B = c(1, 3, 9), C = c(0, 1, 0)), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame"))


 df > 
      A     B     C
1     2     1     0
2     4     3     1
3     6     9     0

I want to reshape it to :

df1 > 
A_1   A_2    A_3    B_1     B_2     B_3    C_1      C_2     C_3
2      4      6      1        3       9      0        1       0
kaix
  • 305
  • 3
  • 10

1 Answers1

0

You can create a row number column in the data and get data in wide format.

library(dplyr)
library(tidyr)

df %>%
  mutate(row = row_number()) %>%
  pivot_wider(names_from = row, values_from = A:C)

#    A_1   A_2   A_3   B_1   B_2   B_3   C_1   C_2   C_3
#  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1     2     4     6     1     3     9     0     1     0
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213