Anatolye Anatolye - 1 month ago 9
R Question

R: Summing of the column values by ranged values of another column

Good day!

I’ve got a table of two columns. In the first column (x) there are values which I want to divide in into categories according to the specified range of values (in my instance – 300). And then using these categories I want to sum values in anther column (v). For instance, using my test data: The first category is from 65100 to 65400 (65100

The result: there is a table of two columns. The first one is the categories of x; the second column is the sum of according values of v.

Thank you!!!

# data
set.seed(1)
x <- sample(seq(65100, 67900, by=5), 100, replace = TRUE)
v <- sample(seq(1000, 8000), 100, replace = TRUE)
tabl <- data.frame(x=c(x), v=c(v))
attach(tabl)
#categories
seq(((min(x) - min(x)%%300) + 300), ((max(x) - max(x)%%300) + 300), by =300)

Answer

Try this (no package needed):

s <- seq(65100, max(tabl$x)+300, 300)
tabl$col = as.vector(cut(tabl$x, breaks = s, labels = 1:10))
df <- aggregate(v~col, tabl, sum)

   # col     v
# 1    1 33234
# 2    2 44805
# 3    3 45493
# 4    4 77865
# 5    5 43887
# 6    6 22893
# 7    7 64079
# 8    8 66517
# 9    9 61738
# 10  10  1722