Search code examples
rdata-manipulation

R: Double Pivots Using DPLYR?


I am working with the R programming language.

I have a dataset that looks something like this:

x = c("GROUP", "A", "B", "C")
date_1 = c("CLASS 1", 20, 60, 82)
date_1_1 = c("CLASS 2", 37, 22, 8)
date_2 = c("CLASS 1", 15,100,76)
date_2_1 = c("CLASS 2", 84, 18,88)

my_data = data.frame(x,  date_1, date_1_1, date_2, date_2_1)

      x  date_1 date_1_1  date_2 date_2_1
1 GROUP CLASS 1  CLASS 2 CLASS 1  CLASS 2
2     A      20       37      15       84
3     B      60       22     100       18
4     C      82        8      76       88
    

I am trying to restructure the data so it looks like this:

  • note : in the real excel data, date_1 is the same date as date_1_1 and date_2 is the same as date_2_1 ... R wont accept the same names, so I called them differently

enter image description here

Currently, I am manually doing this in Excel using different "tranpose" functions - but I am wondering if there is a way to do this in R (possibly using the DPLYR library).

I have been trying to read different tutorial websites online (Pivoting), but so far nothing seems to match the problem I am trying to work on.

Can someone please show me how to do this?

Thanks!


Solution

  • Made assumptions about your data because of the duplicate column names. For example, if the Column header pattern is CLASS_ClassNum_Date

    df<-data.frame(GROUP = c("A", "B", "C"),
                   CLASS_1_1 = c(20, 60, 82),
                   CLASS_2_1 = c(37, 22, 8),
                   CLASS_1_2 = c(15,100,76),
                   CLASS_2_2 = c(84, 18,88))
    library(tidyr)
    pivot_longer(df, -GROUP, 
                 names_pattern = "(CLASS_.*)_(.*)", 
                 names_to = c(".value", "Date"))
    
      GROUP Date  CLASS_1 CLASS_2
      <chr> <chr>   <dbl>   <dbl>
    1 A     1          20      37
    2 A     2          15      84
    3 B     1          60      22
    4 B     2         100      18
    5 C     1          82       8
    6 C     2          76      88
    

    Edit: Substantially improved pivot_longer by using names_pattern= correctly