如何在R中的数据框的列中查找落在特定范围内的项目数
How to find number of items which falls in a specific range in a column of a data frame in R
我有以下数据框:
id weekly_sale
1 40000
2 120000
3 135000
4 211000
5 215000
6 331000
7 337000
我有以下范围:
under 100000
between 100000 and 200000
between 200000 and 300000
more than 300000
它们可以看作是一个向量:
c(0,100000,200000,300000)
我需要计算落入每个范围内的值并生成如下数据框:
under_100000 between_100000_and_200000 between_200000_and_300000 more_than_300000
1 2 2 2
我们可以使用 cut
创建分组,然后使用 table
获取频率。
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
我有以下数据框:
id weekly_sale
1 40000
2 120000
3 135000
4 211000
5 215000
6 331000
7 337000
我有以下范围:
under 100000
between 100000 and 200000
between 200000 and 300000
more than 300000
它们可以看作是一个向量:
c(0,100000,200000,300000)
我需要计算落入每个范围内的值并生成如下数据框:
under_100000 between_100000_and_200000 between_200000_and_300000 more_than_300000
1 2 2 2
我们可以使用 cut
创建分组,然后使用 table
获取频率。
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