Search code examples
rdataframereshape

Reshaping a data frame with example


I am trying to reshape my data frame. I have my data in long columns, and I need to spread that data out into the same row but new columns, as you can see above.

One of the main problems I am running into is creating the new columns. I am going to end up creating almost 300 new columns. In my exact problem I am going to have A1, A2, all the way to A23, same with the other variables, so manually initializing the columns does not seem to be feasible.

Is there a way I can loop or maybe make this a function to create columns and reshape my data frame?

| Column A | Column B | 
| -------- | -------- | 
| Cell 1   | Cell 2   |
| Cell 3   | Cell 4   |
| Cell 5   | Cell 6   |


| Column A | Column B | Column A1 | Column B1 | Column A2| Column B2|
| -------- | -------- | --------- | --------- | ---------| ---------|
| Cell 1   | Cell 2   |  Cell 3   |  Cell 4   | Cell 5   |  Cell 6  |


df$A1 <- NA #Initializing the new column 

df$A1[1] <- df$A[2] #Setting the value in A row 2, to A1 row 1. 

Solution

  • We could do it this way. Main features are creating an id column and grouping after pivoting into long format:

    library(dplyr)
    library(tidyr)
    
    df %>% 
      mutate(id = row_number()) %>% 
      pivot_longer(-id) %>% 
      mutate(name = paste(name, id), .keep = "unused") %>% 
      pivot_wider(names_from = name, values_from = value)
    
      `ColA 1` `ColB 1` `ColA 2` `ColB 2` `ColA 3` `ColB 3`
      <chr>    <chr>    <chr>    <chr>    <chr>    <chr>   
    1 Cell 1   Cell 2   Cell 3   Cell 4   Cell 5   Cell 6  
    

    example data:

    df <- structure(list(ColA = c("Cell 1", "Cell 3", "Cell 5"), ColB = c("Cell 2", 
    "Cell 4", "Cell 6")), class = "data.frame", row.names = c(NA, 
    -3L))