By expanding Tyler’s answer and starting with his example dat, it may be easier and faster for you to write such queries in data.table:
> require(data.table)
> DT = as.data.table(dat)
> DT[, sum(purchase_count), by=cut(view_count,c(0,10,20))]
cut V1
[1,] (10,20] 31
[2,] (0,10] 39
What is it. Just one line. Easy to read, easy to read.
, (10,20], , ( view_count 11 )., by keyby:
> DT[, sum(purchase_count), keyby=cut(view_count,c(0,10,20))]
cut V1
[1,] (0,10] 39
[2,] (10,20] 31
:
> DT[,list( purchase_count = sum(purchase_count) ),
keyby=list( view_count_range = cut(view_count,c(0,10,20) ))]
view_count_range purchase_count
[1,] (0,10] 39
[2,] (10,20] 31