I want sort the values of one data.frame column into predetermined bins, and then sum values that are in the same rows, but a different column.What I'm trying to do is sort dataframe column items into bins based on one value and then get a sum of a second value attached to the items for all of the items in the bin. Can someone help me?
My data looks like this
df =
Item valueX valueY
A 169849631 0.9086560
B 27612064 0.9298379
C 196651878 1.6516654
D 33007984 1.3397873
E 23019448 -0.2954385
F 54779712 -1.6888178
My bins looks like this
Bins=
start end
1 249982
249983 499963
499964 749945
749946 999926
999927 1249907
1249908 1499889
What I want a data frame that looks like this (hypothetical values in frequency and sumvalueY columns)
resultsdf=
binstart binend frequency sumvalueY
1 249982 0 0
249983 499963 5 200
499964 749945 6 400
749946 999926 0 0
999927 1249907 12 30
1249908 1499889 0 0
Here is my code (current iteration)
Start = Bins[,1]
End = Bins[,2]
myfunction <- function(Start,End) {
sum(df$valueX >= Start & df$valueX < End, df[,2])}
Binssorted = mapply(myfunction, Start,End)