Peform Pivot Long to Wider with Multiple Columns in R

11 Views Asked by At

I am trying to transform a data frame from longer to wider, then there are multiple columns in the data frame.

Can someone help me get the right output as shown below or tell me what I am doing wrong.

The data frame I am working with is given below:

structure(list(Material = c("XAB-01-02-03", "XAB-01-02-03", "XAB-01-02-03", 
"XAB-01-02-03", "XAB-01-02-03", "XAB-01-02-03", "XAB-01-02-03", 
"XAB-01-02-03", "XAB-01-02-03", "XAB-01-02-03", "XAB-01-02-03", 
"XAB-01-02-03", "XAB-01-02-03", "XAB-01-02-03", "XAB-01-02-03", 
"XAB-01-02-03", "XAB-01-02-03", "XAB-01-02-03", "XAB-03-05-10", 
"XAB-03-05-10", "XAB-03-05-10", "XAB-03-05-10", "XAB-03-05-10", 
"XAB-03-05-10", "XAB-03-05-10", "XAB-03-05-10", "XAB-03-05-10", 
"XAB-03-05-10", "XAB-03-05-10", "XAB-03-05-10", "XAB-03-05-10", 
"XAB-03-05-10", "XAB-03-05-10", "XAB-03-05-10", "XAB-03-05-10", 
"XAB-03-05-10"), `Calendar Year/Week` = c("2023/28", "2023/29", 
"2023/30", "2023/31", "2023/32", "2023/33", "2023/34", "2023/35", 
"2023/36", "2023/37", "2023/38", "2023/39", "2023/40", "2023/41", 
"2023/42", "2023/43", "2023/44", "2023/45", "2023/28", "2023/29", 
"2023/30", "2023/31", "2023/32", "2023/33", "2023/34", "2023/35", 
"2023/36", "2023/37", "2023/38", "2023/39", "2023/40", "2023/41", 
"2023/42", "2023/43", "2023/44", "2023/45"), Deal = c(0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 3, 22, 26, 
14, 17, 17, 29, 19, 21, 20, 32, 29, 26, 32, 21, 28), Profit = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Comission = c(0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 61, 61, 57, 
57, 57, 57, 68, 68, 68, 68, 81, 81, 81, 81, 81, 96, 96), `Project Commission` = c(0, 
33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 
33, 0, 718, 715, 693, 667, 653, 636, 619, 590, 571, 550, 530, 
498, 469, 443, 411, 390, 362)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -36L))

I got the output but it is not as per the expectation, the code I wrote is :

library(readxl)
library(tidytable)

Book7 <- read_excel("C:/X/X/X- X/X/Book7.xlsx",
                    sheet = "Sheet11")

Book <- Book7 %>% 
  pivot_wider(names_from = `Calendar Year/Week`,
              values_from = Deal:`Project Commission`)

The output, I am getting is:

enter image description here

The output I want is given below:

structure(list(Material = c("XAB-01-02-03", "XAB-01-02-03", "XAB-01-02-03", 
"XAB-01-02-03", "XAB-03-05-10", "XAB-03-05-10", "XAB-03-05-10", 
"XAB-03-05-10"), `Calendar Year/Week` = c("Deal", "Profit", "Comission", 
"Project Commission", "Deal", "Profit", "Comission", "Project Commission"
), `2023/28` = c(0, 0, 0, 0, 0, 0, 0, 0), `2023/29` = c(0, 0, 
0, 33, 10, 0, 61, 718), `2023/30` = c(0, 0, 0, 33, 3, 0, 61, 
715), `2023/31` = c(0, 0, 0, 33, 22, 0, 57, 693), `2023/32` = c(0, 
0, 0, 33, 26, 0, 57, 667), `2023/33` = c(0, 0, 0, 33, 14, 0, 
57, 653), `2023/34` = c(0, 0, 0, 33, 17, 0, 57, 636), `2023/35` = c(0, 
0, 0, 33, 17, 0, 68, 619), `2023/36` = c(0, 0, 0, 33, 29, 0, 
68, 590), `2023/37` = c(0, 0, 0, 33, 19, 0, 68, 571), `2023/38` = c(0, 
0, 0, 33, 21, 0, 68, 550), `2023/39` = c(0, 0, 0, 33, 20, 0, 
81, 530), `2023/40` = c(0, 0, 0, 33, 32, 0, 81, 498), `2023/41` = c(0, 
0, 0, 33, 29, 0, 81, 469), `2023/42` = c(0, 0, 0, 33, 26, 0, 
81, 443), `2023/43` = c(0, 0, 0, 33, 32, 0, 81, 411), `2023/44` = c(0, 
0, 0, 33, 21, 0, 96, 390), `2023/45` = c(0, 0, 0, 33, 28, 0, 
96, 362)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-8L))

The image of the output is given below for clarity:

enter image description here

Thank you in advance.

0

There are 0 best solutions below