简体   繁体   English

如何在 r 中按范围对分类数据进行计数和分组

[英]how to count and group categorical data by range in r

I have data from a questionnaire that has a column for year of birth.我有来自问卷的数据,其中有一列是出生年份。 So the range of data was too large and my mapping became confusing.所以数据范围太大,我的映射变得混乱。 I'm now trying to take the years, group them up by decade decade, and then chart them.我现在正试图把这些年,按十年十年分组,然后绘制图表。 But I don't know how to group them.但我不知道如何将它们分组。

my data is like:我的数据是这样的:

birth_year <- data.frame("years"=c(
  "1920","1923","1930","1940","1932","1935","1942","1944","1952","1956","1996","1961",
  "1962","1966","1978","1987","1998","1999","1967","1934","1945","1988","1976","1978",
  "1951","1986","1942","1999","1935","1920","1933","1987","1998","1999","1931","1977",
  "1920","1931","1977","1999","1967","1992","1998","1984"
))

and my plot is like:我的 plot 是这样的: 在此处输入图像描述

However, I want my data by group as:但是,我希望我的数据按组为:

birth_year   count
(1920-1930]:  5
(1931-1940]:  8
(1941-1950]:  4
(1951-1960]:  3
(1961-1970]:  5
(1971-1980]:  5
(1981-1990]:  5
(1991-2000]:  9

and then plot as a range group.然后 plot 作为范围组。

birth_year %>%
   mutate(val=cut_width(as.numeric(years),10,boundary = 1920, dig.lab=-1))%>%
   count(val)
          val n
1 [1920,1930] 5
2 (1930,1940] 8
3 (1940,1950] 4
4 (1950,1960] 3
5 (1960,1970] 5
6 (1970,1980] 5
7 (1980,1990] 5
8 (1990,2000] 9

We can use cut() to group the data, and then plot with ggplot() .我们可以使用cut()对数据进行分组,然后使用ggplot()对 plot 进行分组。

birth_year <- data.frame("years"=c(
     "1920","1923","1930","1940","1932","1935","1942","1944","1952","1956","1996","1961",
     "1962","1966","1978","1987","1998","1999","1967","1934","1945","1988","1976","1978",
     "1951","1986","1942","1999","1935","1920","1933","1987","1998","1999","1931","1977",
     "1920","1931","1977","1999","1967","1992","1998","1984"
))

birth_year$yearGroup <- cut(as.integer(birth_year$years),breaks = 8,dig.lab = 4,
                            include.lowest = FALSE)
library(ggplot2)

ggplot(birth_year,aes(x = yearGroup)) + geom_bar()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM