Say, for example, I have data on 5 people A:E. These people provide information on what quantity they are willing to buy at what price in four bands. The data are in a wide format.
df = cbind.data.frame(Q1 = c(90,50,20,10,10), Q2 = c(110,0,0,0,0),
Q3 = c(60,60,50,20,5), Q4 = c(20,10,0,0,0),
P1 = 2:6, P2 = c(3,6,8,9,10),
P3 = c(2,3,5,7,9), P4 = 1:5)
row.names(df) = LETTERS[1:5]
(My actual data set has many observations on individuals over many time periods, and with many more bands).
What I wish to do is to sum for each individual, the quantity they are willing to buy within a particular price category.
Say that I want to sum all the quantities an individual is willing to consume at any price within $0 and $5, and similarly for a category of prices between $5 and $10. Using excel I would use a simple 'sumifs' to do such.
How might I do this in R?
The output I would expect from the data above would be:
0<P<=5 5<P<=10
A 280 0
B 120 0
C 70 0
D 10 20
E 0 15
I typically have been using data.table of late, so it would be good if someone knew of a solution using this package. I have also tried doing this by reshaping the data, but it becomes too large to deal with (warning messages etc.) so it needs to remain in this format.
Thanks!