在R中将值从一个 Dataframe 转移到另一个 Dataframe

vxbzzdmp  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(80)

我有两个数据框架:一个名为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
  )

4c8rllxm

4c8rllxm1#

您可以使用dplyr::left_join()进行此操作。我发现它的界面比base::merge()更容易使用。

library(tidyverse)
  
Norway_averageweather_mod <- 
  Norway_averageweather |> 
  select(month, year, Avg_temp, avg_precip, Latitude_bands)

left_join(
  Norway_MM4,
  Norway_averageweather_mod)
#> Joining with `by = join_by(month, year, Latitude_bands)`
#> # A tibble: 8 × 8
#>   Norway_MM4 month  year Latitude Longitude Latitude_bands Avg_temp avg_precip
#>        <dbl> <dbl> <dbl>    <dbl>     <dbl>          <dbl>    <dbl>      <dbl>
#> 1       2008     3  2018     33.2     -81.5             32     33.2       81.5
#> 2       2009     4  2020     33.2     -81.5             32     33.2       61.5
#> 3       2008     3  2018     34.2     -81.5             34     34.4      182. 
#> 4       2007     7  2023     34.2     -81.5             34     34.2       61.3
#> 5       2005     3  2018     36.2     -81.5             36     36.3      810. 
#> 6       1991     8  2010     36.2     -81.5             36     36.2       81.4
#> 7       1991     8  2010     38.2     -71.5             38     37.2       90.4
#> 8       1991     8  2010     40.2     -91.5             40     38.2       91.4

字符串

相关问题