如何将1个数据框中的值分配给另一个数据框中的新列

时间:2020-01-31 02:53:03

标签: r dataframe

我有2个数据框...

第一个数据帧{"spi:action": "OSLC draft", "spi:tri1readingdate":"2020-01-30T16:48:33+01:00", "spi:tryassetmeterid":0, "spi:install":0.0, "spi:lastreadingTx":"1,150", "spi:intrdngtrX":0.0, and so on...} 看起来像这样

mapoc_temp

第二个数据帧 month year ave 1 Jan 2016 6.529222 2 Jan 2017 5.720514 3 Jan 2018 5.786351 4 Feb 2016 6.435445 5 Feb 2017 5.817282 6 Feb 2018 5.790529 7 Mar 2016 6.505259 8 Mar 2017 5.852279 9 Mar 2018 5.683220 10 Apr 2016 6.525603 11 Apr 2017 5.769720 12 Apr 2018 5.762235 13 May 2016 6.425552 14 May 2017 5.855167 15 May 2018 5.778975 16 June 2016 6.488962 17 June 2017 5.871033 18 June 2018 5.720514 mapoc_temp = structure(list(month = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Jan", "Feb", "Mar", "Apr", "May", "June", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"), class = "factor"), year = c(2016, 2017, 2018, 2016, 2017, 2018), ave = c(6.52922242976571, 5.72051368352674, 5.78635119450037, 6.43544457584707, 5.81728212255571, 5.79052889374 )), row.names = c(NA, -6L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), groups = structure(list(month = structure(1:2, .Label = c("Jan", "Feb", "Mar", "Apr", "May", "June", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"), class = "factor"), .rows = list(1:3, 4:6)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE)) 看起来像这样

individual_dets

我想从individual_dets = structure(list(location = c("ARB-04", "BIRCHY HEAD", "Boca1", "BON-AR-S2", "BON-AR-S2", "BON-W-S5"), month = structure(c(12L, 10L, 10L, 8L, 11L, 2L), .Label = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), class = c("ordered", "factor")), year = c(2018, 2018, 2018, 2018, 2018, 2018), detection_count = c(3L, 256L, 2L, 4L, 2L, 2L), num_unique_tags = c(1L, 1L, 1L, 1L, 1L, 1L), total_res_time_in_seconds = c(0, 1182040, 0, 2732221, 0, 0), latitude = c(24.94808, 44.5713, 26.32559, -49.27732, -49.27732, -49.27985), longitude = c(-80.45412, -64.03512, -80.07108, 69.48038, 69.48038, 69.47853), zone = structure(c(4L, 4L, 4L, 4L, 4L, 4L ), .Label = c("1", "2", "3", "4"), class = "factor"), ave_temp = c(5.740993, 5.855167, 5.855167, 5.852279, 5.871033, 5.790529)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), groups = structure(list( month = structure(c(2L, 8L, 10L, 11L, 12L), .Label = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), class = c("ordered", "factor")), .rows = list( 6L, 4L, 2:3, 5L, 1L)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE)) 数据框中的ave列中获取值,并确保将这些值分配给mapoc_dets中的相应月份和年份

我尝试使用以下代码,但我希望有人可能知道如何缩短此代码

individual_dets

1 个答案:

答案 0 :(得分:0)

ungroup设置了数据集并将列转换为要合并到同一class之后,我们可以使用left_join

library(dplyr)
out <- individual_dets %>% 
         ungroup %>%
         mutate(month = as.character(month)) %>%
         left_join(mapoc_temp %>%
           ungroup %>%
           mutate(month = as.character(month)), by = c("year", "month") )