R 函数镜像 COUNTIFS 产生错误的结果



我想得出的结果之前已经在这个问题中处理过:是否有一个 R 函数镜像 EXCEL 计数,以日期范围为条件?

我有两个单独的数据帧作为输入:TOTALREVTOTALLISTINGS,它们都以简化的形式如下所示:

TOTALREV
listing_id reviewer_id reviewer_name review_date
1       2818       10952           Lam  2009-03-30
2       2818       12798         Alice  2009-04-24
3       2818       11869       Natalja  2009-05-03
4       2818       14064       Enrique  2009-05-18
5       2818       17977       Sherwin  2009-05-25
6       2818       20192           Jie  2009-06-29

TOTALLISTINGS
listing_id last_scraped.calc
1       2818        2019-03-07
2      20168        2019-03-07
3      25428        2019-03-07
4      27886        2019-03-07
5      28658        2019-03-07
6      28871        2019-03-07

请注意,我每listing_id有多个last_scraped.calc

所以我实际需要的是一个代码来计算所有review_date条目,其中TOTALLISTINGSlisting_idTOTALREV中的listing_id匹配,而来自TOTALREVreview_date从相应的last_scraped.calc最多 30 天TOTALLISTINGS

这样我的预期输出将是:

REVIEWCOUNT
listing_id last_scraped.calc reviews_last30
<dbl>    <date>                   <int>
1     1   2016-11-15                   1
2     1   2016-11-20                   1
3     2   2016-11-15                   3
4     2   2016-11-20                   2

在上一个线程中,"mfidino"已经帮助我想出了以下代码,这些代码曾经工作正常,直到我编译了一些完全相同类型的额外数据:

library(lubridate)
library(dplyr)
genlistings <- function(TOTALLISTINGS = NULL, TOTALREV = NULL){
# tibble to return
to_return <- TOTALREV %>%
inner_join(., TOTALLISTINGS, by ='listing_id') %>% 
group_by(listing_id, last_scraped.calc) %>% 
summarise(
reviews_last30 = sum((review_date >= (last_scraped.calc-30) & (review_date <= last_scraped.calc))))
return(to_return)
}
REVIEWCOUNT <- genlistings(TOTALREV, TOTALLISTINGS)

但是,当现在运行上述代码时,我的 R 仅返回以下内容,而不是上面指示的建议输出REVIEWCOUNT

head(REVIEWCOUNT)
reviews_last30
1        1018668

所以不幸的是,我认为代码并没有真正按listing_idlast_scraped.calc分组,而只是总结了上述条件为真的所有评论。

非常感谢任何帮助 - 提前感谢!

您可以先merge两个表,然后在不超过 30 天的表上使用table来获取频率。(我已将最大天数从 30 更改为 3580,以便从您的示例数据中获得输出。

TOTALREV <- read.table(header=TRUE, text="listing_id reviewer_id reviewer_name review_date
1       2818       10952           Lam  2009-03-30
2       2818       12798         Alice  2009-04-24
3       2818       11869       Natalja  2009-05-03
4       2818       14064       Enrique  2009-05-18
5       2818       17977       Sherwin  2009-05-25
6       2818       20192           Jie  2009-06-29")
TOTALLISTINGS <- read.table(header=TRUE, text="listing_id last_scraped.calc
1       2818        2019-03-07
2      20168        2019-03-07
3      25428        2019-03-07
4      27886        2019-03-07
5      28658        2019-03-07
6      28871        2019-03-07")
#Change to Date to allow calculating difference
TOTALREV$review_date <- as.Date(TOTALREV$review_date)
TOTALLISTINGS$last_scraped.calc <- as.Date(TOTALLISTINGS$last_scraped.calc)
me <- merge(TOTALREV, TOTALLISTINGS,)
#maxDays <- 30
maxDays <- 3580
data.frame(with(me[me$review_date - me$last_scraped.calc <= 0 & me$review_date - me$last_scraped.calc >= -maxDays,], table(listing_id, last_scraped.calc)))
#  listing_id last_scraped.calc Freq
#1       2818        2019-03-07    3

最新更新