I have two dataframes: a parent dataframe called Norway_MM4 with 57043 rows, and another dataframe called Norway_averageweather.
I will like to merge both dataframes, Norway_averageweather has unique month, year, and latitudes_band columns, but Norway_MM4 does not have unique entries for these dates.
I want to match the average temperature entries in the weather data to each date and latitude_bands on the parent dataframe
Norway_MM4 <-
dplyr::tribble(
~date, ~month, ~year, ~Latitude, ~Longitude, ~Latitude_bands,
2018-03-07, 03, 2018, 33.21580, -81.52677, 32,
2020-04-07, 04, 2020, 33.21608, -81.52699, 32,
2018-03-07, 03, 2018, 34.21592, -81.52706, 34,
2023-07-09, 07, 2023, 34.21521, -81.53458, 34,
2018-03-10, 03, 2018, 36.21737, -81.53036, 36,
2010-08-11, 08, 2010, 36.21171, -81.53041, 36,
2010-08-11, 08, 2010, 38.21171, -71.53041, 38,
2010-08-11, 08, 2010, 40.21167, -91.53041, 40
)
Norway_averageweather <-
dplyr::tribble(
~date, ~month, ~year, ~Avg_temp, ~avg_precip, ~Latitude_bands,
2018-03-07, 03, 2018, 33.21, 81.52 ,32 ,
2020-04-07, 04, 2020, 33.21, 61.50 ,32 ,
2018-05-07, 03, 2018, 34.41, 181.52, 34,
2023-07-09, 07, 2023, 34.21, 61.34 ,34 ,
2018-03-10, 03, 2018, 36.31, 810.36, 36,
2010-08-11, 08, 2010, 36.21, 81.41 ,36 ,
2010-08-11, 08, 2010, 37.21, 90.41 ,38 ,
2010-08-11, 08, 2010, 38.21, 91.41 ,40
)
Here is what I tried so far:
merged_df <- merge(unique(Norway_MM4),
Norway_averageweather,
by = c("Latitude_Bands", "month", "year"),
all.x = TRUE)
And this is what I would like the result to look like:
merged_df <-
dplyr::tribble(
~date, ~month, ~year, ~Latitude, ~Longitude, ~Latitude_bands, ~Avg_temp, ~avg_precip,
2018-03-07, 03, 2018, 33.21580, -81.52677, 32, 33.21, 81.52 ,
2020-04-07, 04, 2020, 33.21608, -81.52699, 32, 33.21, 61.50,
2018-03-07, 03, 2018, 34.21592, -81.52706, 34, 34.41, 181.52,
2023-07-09, 07, 2023, 34.21521, -81.53458, 34, 34.21, 61.34 ,
2018-03-10, 03, 2018, 36.21737, -81.53036, 36, 36.31, 810.36,
2010-08-11, 08, 2010, 36.21171, -81.53041, 36, 36.21, 81.41 ,
2010-08-11, 08, 2010, 38.21171, -71.53041, 38, 37.21, 90.41 ,
2010-08-11, 08, 2010, 40.21167, -91.53041, 40, 38.21, 91.41
)