我有两个数据框架:一个名为Norway_MM4的父数据框,有57043行,另一个名为Norway_averageweather的数据框。
我想合并两个数据框架,Norway_averageweather有唯一的month、year和latitudes_band列,但Norway_MM4没有这些日期的唯一条目。
我希望将天气数据中的平均温度条目与父数据框上的每个日期和纬度波段进行匹配
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
)
字符串
以下是我到目前为止所尝试的:
merged_df <- merge(unique(Norway_MM4),
Norway_averageweather,
by = c("Latitude_Bands", "month", "year"),
all.x = TRUE)
型
这就是我希望得到的结果
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
)
型
1条答案
按热度按时间4c8rllxm1#
您可以使用
dplyr::left_join()
进行此操作。我发现它的界面比base::merge()
更容易使用。字符串