R 如何将1个数据帧中的值分配给另一个数据帧中的新列
我有两个数据帧 第一个数据帧,R 如何将1个数据帧中的值分配给另一个数据帧中的新列,r,dataframe,R,Dataframe,我有两个数据帧 第一个数据帧,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.85
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 = 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))
我的第二个数据帧,individual_dets
,如下所示
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 = 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
dataframe中的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
))))))))))))
)))))))))))))
))))))))))))
在
解组
数据集并将要联接的列转换为同一类
之后,我们可以使用左联接
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") )
库(dplyr)
超出%
解组%>%
变异(月=作为.字符(月))%>%
左联合(mapoc\U临时%>%
解组%>%
变异(月=as.character(月)),by=c(“年”,“月”)
我将创建一个keyval数据并进行连接,即交叉(month=month.abb,year=2016:2018)%%>%mutate(value=..)%%>%right\u join(originaldata)
在单个数据中出现的ave\u temp
列在运行您所拥有的代码后根本不会改变。您希望如何从mapoc\u temp
获取ave
值?按月
或年
或两者?月和年都给了我这个列,但都是na's@KristenCyr它基于一个示例,其中一个数据集中不存在“年”、“月”的组合。