r - 加入R时总结

标签 r dplyr inner-join summarize

我有两个数据集,我想连接这两个数据集并同时应用汇总命令。

示例数据:

数据1:我们在三个时间点(obs_id)观察三个产品(id)以及该产品的评论数量(n_join)。

product_data = data.frame(id = c(rep("product1", 3), rep("product2", 3),rep("product3", 3)), obs_id = rep(c(1,2,3), 3), n_join = c(1,3,5,1,1,2,1,2,4))

product_data
        id obs_id n_join
1 product1      1      1
2 product1      2      3
3 product1      3      5
4 product2      1      1
5 product2      2      1
6 product2      3      2
7 product3      1      1
8 product3      2      2
9 product3      3      4

数据 2:我们观察相同的三个产品 (id),每行反射(reflect)一条评论 (review_id),并且对于每条评论,如果包含购买意向 (purchase_intention),则显示一个二元指示器 (1 = 是)。

review_data = data.frame(id = c(rep("product1", 5), rep("product2", 2),rep("product3", 4)), 
                          review_id = c(1,2,3,4,5,1,2,1,2,3,4),
                          purchase_intention = c(1,1,1,0,1,0,1,0,0,1,1))
 
review_data
         id review_id purchase_intention
1  product1         1                  1
2  product1         2                  1
3  product1         3                  1
4  product1         4                  0
5  product1         5                  1
6  product2         1                  0
7  product2         2                  1
8  product3         1                  0
9  product3         2                  0
10 product3         3                  1
11 product3         4                  1

现在我想通过以下方式将评论数据加入到产品数据中: 我想在 Product_data 中创建一个新列,用于指示包括购买意向在内的评论数量。

示例:

  • 要为product_data第一行创建新的“sum_purchase_intention”列,我需要获取review_data中purchase_intention列的第一行(由product_data中的n_join表示)的值:1。
  • 要为product_data第二行创建新的“sum_purchase_intention”列,我需要获取review_data中purchase_intention列的第一行和第二行(由product_data中的n_join表示)的值:1 + 1 + 1 = 3.
  • 要为product_data第三行创建新的“sum_purchase_intention”列,我需要获取review_data中purchase_intention列的第1-5行(由product_data中的n_join表示)的值:1 + 1 + 1 + 0 + 1 = 4。

因此,预期结果如下所示(注意数据需要按 id 分组):

final_data = data.frame(id = c(rep("product1", 3), rep("product2", 3),rep("product3", 3)), obs_id = rep(c(1,2,3), 3), n_join = c(1,3,5,1,1,2,1,2,4),
                         sum_purchase_intentions = c(1,3,4,0,0,1,0,0,2))
final_data
        id obs_id n_join sum_purchase_intentions
1 product1      1      1                       1
2 product1      2      3                       3
3 product1      3      5                       4
4 product2      1      1                       0
5 product2      2      1                       0
6 product2      3      2                       1
7 product3      1      1                       0
8 product3      2      2                       0
9 product3      3      4                       2

我认为这应该可以通过 dplyr 包的inner_join和summary命令的组合来实现,但是我没有成功地将它们组合起来。有人可以帮忙吗?

最佳答案

library(dplyr); library(tidyr)
product_data %>%
  left_join(review_data, by = "id") %>%
  filter(n_join >= review_id) %>%
  count(id, obs_id, n_join, wt = purchase_intention)

#        id obs_id n_join n
#1 product1      1      1 1
#2 product1      2      3 3
#3 product1      3      5 4
#4 product2      1      1 0
#5 product2      2      1 0
#6 product2      3      2 1
#7 product3      1      1 0
#8 product3      2      2 0
#9 product3      3      4 2

这是“非等值连接”的示例,您希望将原始表中的每一行与第二个表中不同的数据窗口连接起来。 dplyr 目前不提供该功能,但对于小数据,通常可以对所有潜在相关数据进行联接,然后过滤掉窗口外的项目。

为了更直接地执行此操作,fuzzyjoinsqldfdata.table 提供非等值联接。例如,使用 fuzzyjoin 的代码对于大型数据集会更有效,因为它只会连接与给定 id 相关的 review_datan_join 号码。当每个id有很多评论时,这将减少中间数据集大小爆炸的趋势。

library(fuzzyjoin)
product_data %>%
  fuzzy_left_join(review_data, 
                  by = c("id" = "id", "n_join" = "review_id"),
                  match_fun = list(`==`, `>=`)) %>%
  count(id = id.x, obs_id, n_join, wt = purchase_intention) 

关于r - 加入R时总结,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70582226/

相关文章:

r - dplyr 中的 mutate_each/summarise_each : how do I select certain columns and give new names to mutated columns?

r - 使用 purrr::map 将多个参数应用于函数

mysql - 从出现的表中计数记录是一个而不是其他 : MYSQL

mysql - MySQL 上的子查询

mysql - Doctrine 2 发出了不需要的查询,或者也许我错了,但这是必要的?

r - 在多个条件下过滤 Shiny 的数据表

r - 从Linux上的本地.tar.gz文件安装软件包

r - 在 R 中预测 Arima 模型返回奇怪的错误

r - 在函数中获取过滤器以进行整洁的评估

r - 具有多个条件和 OR 的 dplyr 过滤器