I want to add a seasonal_factor column to my D1 data frame. The seasonal factor is from another source, in matrix format, with 4 matrices per year from 2021 to 2024.
I get errors on matching the same year, Month, and Weekday because the variables are mixed with row names, column names, and column values. (I added the year as a new column that combines four years' matrices into one matrix.) Can I get some suggestions on how to re-write a suitable code?
D1 <- structure(list(year = c("2021", "2021", "2021", "2021", "2021"), Month = c("Apr", "Apr", "Apr", "Apr", "Apr"), Weekday = c("Monday","Monday", "Monday", "Monday", "Thursday"), hour = c("07", "08", "16", "17", "07"), avg_speed = c(40.2, 38.3, 40, 40.1, 39.6), avg_volume = c(2612, 2389, 2108, 1948, 2612)), row.names = c(NA, 5L), class = c("tbl_df", "tbl", "data.frame"))
Matrices have been converted into one data frame with year as last column:
lookup_df <- structure(list(Weekday = c("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday"), Jan = c(1.816, 1.123, 1.15, 1.089, 1.065,
1.03), Feb = c(1.617, 1.122, 1.171, 1.05, 1.045, 1.031), Mar = c(1.428,
1.018, 0.988, 0.96, 0.97, 0.919), Apr = c(1.354, 1, 0.966, 0.956,
0.956, 0.907), May = c(1.233, 0.949, 0.917, 0.897, 0.88, 0.863
), Jun = c(1.158, 0.919, 0.896, 0.878, 0.852, 0.848), Jul = c(1.177,
0.957, 0.896, 0.866, 0.855, 0.852), Aug = c(1.145, 0.909, 0.881,
0.868, 0.853, 0.831), Sep = c(1.168, 0.91, 0.907, 0.889, 0.86,
0.82), Oct = c(1.234, 0.942, 0.899, 0.883, 0.871, 0.845), Nov = c(1.364,
0.944, 0.922, 0.901, 0.905, 0.855), Dec = c(1.358, 1.011, 0.971,
0.945, 0.938, 0.931), year = c(2021, 2021, 2021, 2021, 2021,
2021)), row.names = c(NA, 6L), class = "data.frame")
D2 <- left_join(D1, lookup_df, by = c("year", "Weekday", "Month"))
Error in
left_join(): Join columns inymust be present in the data. Problem withMonth. Runrlang::last_trace()to see where the error occurred.
dput, there might be a more streamlined way to go from them toD2.left_join(.)something and they did not understand the error. They provided sample data, the code they ran, and the error that came of. Previously stated was the expectation of bring values from one matrix (which is no longer a matrix, but I'm digressing) into the target frame. Other than being a little distracting with "matrix" and no matrix, this has most of the components of a single question and min-reprex. The fact that some of us immediately see the pivot/join need is part of the learning, isn't it?