gpt4 book ai didi

r - 挣扎于如何组合数据框中的行组

转载 作者:行者123 更新时间:2023-12-04 12:35:17 24 4
gpt4 key购买 nike

我有这样一个数据框

product_id view_count purchase_count
1 11 1
2 20 3
3 5 2
...

例如,我想将其转换成一个表格,该表格按 view_count 分组并对一个时间间隔内的 purchase_count 求和。

view_count_range total_purchase_count
0-10 45
10-20 65

这些 view_count_ranges 将具有固定大小。对于如何对这样的范围进行分组的任何建议,我将不胜感激。

最佳答案

cut 是处理这类事情的便捷工具。这是一种方法:

#First make some data to work with 
#I suggest you do this in the future as it makes it
#easier to provide you with assistance.
set.seed(10)
dat <- data.frame(product_id=1:15, view_count=sample(1:20, 15, replace=T),
purchase_count=sample(1:8, 15, replace=T))
dat #look at the data

#now we can use cut and aggregate by this new variable we just created
dat$view_count_range <- with(dat, cut(view_count, c(0, 10, 20)))
aggregate(purchase_count~view_count_range, dat, sum)

哪些 yield :

  view_count_range purchase_count
1 (0,10] 39
2 (10,20] 31

关于r - 挣扎于如何组合数据框中的行组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10593136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com