我们说data.frame
名为countDF
:
> countDF
date count complete
1 20180124 16 FALSE
2 20180123 24 TRUE
3 20180122 24 TRUE
4 20180121 24 TRUE
5 20180120 23 FALSE
6 20180119 23 FALSE
7 20180118 24 TRUE
看起来像这样:
> dput(countDF)
structure(list(date = c("20180124", "20180123", "20180122", "20180121",
"20180120", "20180119", "20180118"), count = c(16L, 24L, 24L,
24L, 23L, 23L, 24L), complete = c(FALSE, TRUE, TRUE, TRUE, FALSE,
FALSE, TRUE)), class = "data.frame", row.names = c(NA, -7L), .Names = c("date",
"count", "complete"))
这个清单:
> last7D_missingHours
$`20180124`
[1] 3 17 18 19 20 21 22 23
$`20180120`
[1] 18
$`20180119`
[1] 7
看起来像这样:
> dput(last7D_missingHours)
structure(list(`20180124` = c(3L, 17L, 18L, 19L, 20L, 21L, 22L,
23L), `20180120` = 18L, `20180119` = 7L), .Names = c("20180124",
"20180120", "20180119"))
我想建一个data.frame
(或者,data_frame
)将left_join(countDF, last7D_missingHours, by = c('date' = names(last7D_missingHours)))
加入前者NA
并date
> countDF
date count complete missingHour
1 20180124 16 FALSE 3 17 18 19 20 21 22 23
2 20180123 24 TRUE NA
3 20180122 24 TRUE NA
4 20180121 24 TRUE NA
5 20180120 23 FALSE 18
6 20180119 23 FALSE 7
7 20180118 24 TRUE NA
不匹配的行,如下所示:
tibbles
我可能会通过我猜测的递归子集来解决这个问题,但是想知道是否有人对更优化的方法有任何建议,因为我知道(db.query(TableA)
.filter(TableA.id == TableB.table_a_id,
TableA.record_id.is_(None))
.update({TableA.record_id: TableB.record_id}, synchronize_session=False))
最近已经走了很长的路。 ..
答案 0 :(得分:1)
将缺少的小时数放入tibble
的列表列中,其他变量作为日期,然后只有left_join
。
library(tidyverse)
countDF <- structure(list(date = c("20180124", "20180123", "20180122", "20180121",
"20180120", "20180119", "20180118"),
count = c(16L, 24L, 24L, 24L, 23L, 23L, 24L),
complete = c(FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE)),
class = "data.frame", row.names = c(NA, -7L), .Names = c("date", "count", "complete"))
last7D_missingHours <- structure(list(`20180124` = c(3L, 17L, 18L, 19L, 20L, 21L, 22L,
23L), `20180120` = 18L, `20180119` = 7L), .Names = c("20180124",
"20180120", "20180119"))
lst_tbl <- tibble(date = c("20180124", "20180120", "20180119"),
missingHour = last7D_missingHours)
left_join(countDF, lst_tbl)
#> Joining, by = "date"
#> date count complete missingHour
#> 1 20180124 16 FALSE 3, 17, 18, 19, 20, 21, 22, 23
#> 2 20180123 24 TRUE NULL
#> 3 20180122 24 TRUE NULL
#> 4 20180121 24 TRUE NULL
#> 5 20180120 23 FALSE 18
#> 6 20180119 23 FALSE 7
#> 7 20180118 24 TRUE NULL
我最终得到的是NULL
而不是NA
,我觉得这更有意义,所以我没有尝试改变它们只是为了得到你要求的东西。