Am trying to convert a table from "wide form" to "long form", but in this case create new categories based on current values.
Basically, am trying to "unpivot" a roster, as my colleague mentioned is the right term. The Rows are dates, and the columns are personnel names. However, the values are "shifts" represented by shift names like (X, O, M1).
Want to move from "before" to "after"
Any advice on a simple solution or how to better describe my question to find preexisting answers ?
FJCC
March 15, 2021, 2:31am
2
This give you basically what you want. The pivot_longer() function does the important reshaping and the select() is only used to reorder the columns.
DF <- data.frame(Date=c("1-Mar-2021","2-Mar-2021","3-Mar-2021","4-Mar-2021","5-Mar-2021",
"6-Mar-2021","7-Mar-2021"),
AC3=c("OS","X","C1","NO","M1","M2","O"),
C13=c("O","PDL","X","A3f","E2","O","O"))
library(tidyr)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
DF_new <- pivot_longer(DF,cols = c("AC3","C13"),values_to = "Shift") %>%
select(Date,Shift,name)
DF_new
#> # A tibble: 14 x 3
#> Date Shift name
#> <chr> <chr> <chr>
#> 1 1-Mar-2021 OS AC3
#> 2 1-Mar-2021 O C13
#> 3 2-Mar-2021 X AC3
#> 4 2-Mar-2021 PDL C13
#> 5 3-Mar-2021 C1 AC3
#> 6 3-Mar-2021 X C13
#> 7 4-Mar-2021 NO AC3
#> 8 4-Mar-2021 A3f C13
#> 9 5-Mar-2021 M1 AC3
#> 10 5-Mar-2021 E2 C13
#> 11 6-Mar-2021 M2 AC3
#> 12 6-Mar-2021 O C13
#> 13 7-Mar-2021 O AC3
#> 14 7-Mar-2021 O C13
Created on 2021-03-14 by the reprex package (v0.3.0)
Exactly what I needed
Working beautifully on the big huge roster!
Thanks!!
system
Closed
April 5, 2021, 4:27am
4
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.