I have a df akin to df1 where I want to break out the rows so that the Hrs_Time_Worked column is in intervals of 4, shown in df2.
I have been using the following code, but it throws an error:
df2 = df1 %>%
group_by(Row)%>%
mutate(S=START_DATE_TIME,
Hrs_Time_Worked=list((n<-c(rep(4,Hrs_Time_Worked%/%4),Hrs_Time_Worked%%4))[n!=0]))%>%
unnest()%>%
mutate(E=START_DATE_TIME+hours(cumsum(Hrs_Time_Worked)),
S=E-hours(unlist(Hrs_Time_Worked)),
START_DATE_TIME=(S),
END_DATE_TIME=(E),
S=NULL,E=NULL)
Error in mutate_impl(.data, dots) : Evaluation error: invalid class Period object: periods must have integer values.
The following are required:
All categorical data must remain the same on child rows (e.g., TIME_RPTG_CD stays the same on every child row)
If there is a remainder that is less than four, the remainder amount should be listed on the last line (e.g., df2; row 3)
If a child row starts or ends on the next date the date column should be updated accordingly (e.g., df2; row 2-3)
df1 (current)
Row EMPLID TIME_RPTG_CD START_DATE_TIME END_DATE_TIME Hrs_Time_Worked
<chr> <chr> <dttm> <dttm> <dbl>
1 X00007 REG 2014-07-03 16:00:00 2014-07-03 02:00:00 10.0
df2 (desired)
Row EMPLID TIME_RPTG_CD START_DATE_TIME END_DATE_TIME Hrs_Time_Worked
<chr> <chr> <dttm> <dttm> <dbl>
1 X00007 REG 2014-07-03 16:00:00 2014-07-03 20:00:00 4.0
2 X00007 REG 2014-07-03 20:00:00 2014-07-04 24:00:00 4.0
3 X00007 REG 2014-07-04 24:00:00 2014-07-04 02:00:00 2.0