Reputation: 3419
I have the following data frame:
id weekly_sale
1 40000
2 120000
3 135000
4 211000
5 215000
6 331000
7 337000
and i have the following ranges:
under 100000
between 100000 and 200000
between 200000 and 300000
more than 300000
they can be seen as a vector :
c(0,100000,200000,300000)
I need to count the the values that fall into each range and result a data frame like this:
under_100000 between_100000_and_200000 between_200000_and_300000 more_than_300000
1 2 2 2
Upvotes: 2
Views: 80
Reputation: 887501
We can use cut
to create the grouping and then with table
get the frequency.
with(df1, table(cut(weekly_sale, breaks = c(-Inf,100000, 200000,
300000, Inf), labels = c("under 100000", "between 100000 and 200000",
"between 200000 and 300000", "more than 300000"))))
# under 100000 between 100000 and 200000 between 200000 and 300000 more than 300000
1 2 2 2
Upvotes: 3