Search code examples
rcastingreshapemelt

how to Cast part of the df but still keeping some other columns


What I want to do is create a new column that split task billable and allocate taskHRS to that columns (cast) and allocate the hours into true and false without affecting the other columns.

Code that I am using but doesn't get the desired outcome:

 Exp <- Timesheets %>%
     mutate(taskDate = as.Date(taskDate)) %>%
     mutate(taskMinutes = as.numeric(taskMinutes)) %>%
     select('jobID', 'jobTaskID', 'taskStaffName', 'taskDate', 'taskBillable','taskMinutes', 'which_payday') %>%
     group_by(jobID, taskStaffName, taskDate, taskBillable, which_payday) %>%
     summarise(taskHRS = sum(taskMinutes/60)) %>%
     filter(which_payday %in% c('ThisFN'))
print(Exp)

Exp <- Exp %>%
cast(taskStaffName ~ taskBillable ~ jobID, value = 'taskHRS')

Desired Outcome Sample:

JobID  taskStaffName  taskDate   true false which_payday Budget
W20100        L       2/10/2018    2     1   ThisFN       80

Data Sample:

jobID   taskStaffName taskDate   taskBillable which_payday taskHRS Budget
       <chr>   <chr>         <date>     <chr>        <chr>          <dbl>  <dbl>
     1 W210125 L             2018-11-13 true         ThisFN           1.5     80
     2 W210125 R             2018-11-12 true         ThisFN           2       80
     3 W210125 R             2018-11-13 true         ThisFN           2       80
     4 W210125 T             2018-11-13 true         ThisFN           2       80
     5 W210233 C             2018-11-12 true         ThisFN           6       80

Solution

  • Well, I found a solution for this problem. I have create the following code that achieve my desired outcome.

    library(tidyr)

     Exp <- Timesheets %>%
         mutate(taskDate = as.Date(taskDate)) %>%
         mutate(taskMinutes = as.numeric(taskMinutes)) %>%
         select('jobID', 'jobTaskID', 'taskStaffName', 'taskDate', 'taskBillable','taskMinutes', 'which_payday') %>%
         group_by(jobID, taskStaffName, taskDate, taskBillable, which_payday) %>%
         summarise(taskHRS = sum(taskMinutes/60)) 
    
     library(tidyr)    
    
    Exp <- Exp %>%
         spread(taskBillable, taskHRS)
    

    I hope this can help anyone that is dealing with a similar problem.

    The outcome that I got is as I want it

    JobID  taskStaffName  taskDate   true false which_payday Budget
    W20100        L       2/10/2018    2     1   ThisFN       80*