df1
...other columns...MON TUE WED THU FRI SAT SUN Total
8.5 8.5 8.5 8.5 8.5 6.0 0.0 0.0
9.0 9.0 9.0 9.0 9.0 6.0 6.0 0.0
6.0 7.0 7.0 7.0 7.0 5.0 0.0 0.0
...about 1400 records/rows of data in df1
df2
Day Hours
FRI 0
SAT 0
SUN 0
MON 0
TUE 0
WED 0
THU 0
FRI 0
SAT 0
SUN 0
MON 0
TUE 0
WED 0
...Will keep going until end (28-31 days worth will be listed - all days in a month)
Step 1
I need to loop through all lines in df2, matching all records in column 'Day' with df1 columns MON-SUN...adding the corresponding hours in df1 to the 'Hours' column in df2...using only the first row in df1 until all of df2 has been looped through and filled...Output example below
df2 output
Day Hours
FRI 8.5
SAT 6.0
SUN 0
MON 8.5
TUE 8.5
WED 8.5
THU 8.5
FRI 8.5
SAT 6.0
SUN 0
MON 8.5
TUE 8.5
WED 8.5
...will continue all the way until last row until all data is filled from 1st row in df1 (repeating itself, just matching the right values)
2nd step
After df2 has been looped and filled - then sum of 'Hours' column in df2 and place in df1 'total' column
df1 output
...other columns...MON TUE WED THU FRI SAT SUN Total
8.5 8.5 8.5 8.5 8.5 6.0 0.0 88.5
9.0 9.0 9.0 9.0 9.0 6.0 6.0
6.0 7.0 7.0 7.0 7.0 5.0 0.0
This then repeats until all rows in df1 has been looped and has gone through the same process...so probably a double loop of some sort is needed with a match function. I am struggling to find any solutions to this
Code used
row_df1 <- 1
row_df2 <- 1
for (row_df2 in seq(1,nrow(Calendar$Jan))) {
for (day in week) {
if (Calendar$Jan[row_df2, 'Day'] == day) {
Calendar$Jan[row_df2,'Hours'] <- Calctable[row_df1,day]
row_df2 <- row_df2 + 1
}
}
}
Error Message
Error in for (day in week) { : invalid for() loop sequence
Many thanks