[英]how to assign values from 1 dataframe to a new column in another dataframe
我有2个数据框...
第一个数据帧, 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))
我想从mapoc_dets
数据mapoc_dets
ave
列中获取值,并确保将这些值分配给individual_dets
的相应月份和年份
我尝试使用以下代码,但我希望有人可能知道如何缩短此代码
individual_dets = individual_dets %>%
group_by(month) %>%
mutate(ave_temp =
if_else(month == "Jan" && year == 2016 , 6.529222,
if_else (month == "Feb" && year == 2016, 6.435445,
if_else(month == "Mar" && year == 2016, 6.505259,
if_else(month == "Apr" && year == 2016, 6.525603,
if_else (month == "May" && year == 2016, 6.425552,
if_else (month == "Jun" && year == 2016, 6.488962,
if_else (month == "Jul" && year == 2016, 6.490498,
if_else (month == "Aug" && year == 2016, 6.417815,
if_else (month == "Sep" && year == 2016, 6.492893,
if_else (month == "Oct" && year == 2016, 6.502256,
if_else (month == "Nov" && year == 2016, 6.427294,
if_else (month == "Dec" && year == 2016, 6.508574,
if_else(month == "Jan" && year == 2017 , 5.720514,
if_else (month == "Feb" && year == 2017, 5.817282,
if_else(month == "Mar" && year == 2017, 5.852279,
if_else(month == "Apr" && year == 2017, 5.769720,
if_else (month == "May" && year == 2017, 5.855167,
if_else (month == "Jun" && year == 2017, 5.871033,
if_else (month == "Jul" && year == 2017, 5.740993,
if_else (month == "Aug" && year == 2017, 5.786351,
if_else (month == "Sep" && year == 2017, 5.790529,
if_else (month == "Oct" && year == 2017, 5.683220,
if_else (month == "Nov" && year == 2017, 5.762235,
if_else (month == "Dec" && year == 2017, 5.778975,
if_else(month == "Jan" && year == 2018 , 5.786351,
if_else (month == "Feb" && year == 2018, 5.790529,
if_else(month == "Mar" && year == 2018, 5.683220,
if_else(month == "Apr" && year == 2018, 5.762235,
if_else (month == "May" && year == 2018, 5.778975,
if_else (month == "Jun" && year == 2018, 5.720514,
if_else (month == "Jul" && year == 2018, 5.817282,
if_else (month == "Aug" && year == 2018, 5.852279,
if_else (month == "Sep" && year == 2018, 5.769720,
if_else (month == "Oct" && year == 2018, 5.855167,
if_else (month == "Nov" && year == 2018, 5.871033,
if_else (month == "Dec" && year == 2018, 5.740993, 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") )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.