在表中创建 bin
Creating bins in tables
我有一组数据,按年份 (raw$Year)
显示测试分数 (raw$Score)
。
我想创建一个 table,在其中我将年份视为行,将分数视为列,并将分数以 5 为增量进行分组。
当我 运行table(raw$Year,raw$Score)
时,我得到以下信息。我将如何修改 table(raw$Year,raw$Score)
以使计数按 75-80、81-85、86-90、91、-95 和 96-100 分组?
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
2006 0 1 0 0 1 3 1 0 1 1 1 1 0 0 1 0 0 0 2 1 0 2 0 1 1 6
2007 0 0 1 0 1 1 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 4 2 2 1 5
2008 0 1 2 0 2 2 1 2 1 0 0 0 0 0 0 1 2 0 0 0 1 0 2 3 2 8
2009 1 1 3 0 1 1 6 0 2 1 1 3 1 0 0 2 0 1 0 0 0 1 2 2 1 13
2010 2 5 0 2 3 1 1 3 2 1 1 0 2 1 2 2 1 0 0 2 2 2 2 2 4 24
2011 2 1 1 0 3 1 0 1 2 0 0 1 2 1 1 2 3 0 2 1 2 0 0 2 3 12
2012 1 1 3 5 4 1 1 1 1 0 2 0 1 1 0 1 0 0 1 2 1 0 3 1 2 24
2013 2 2 3 3 1 2 2 3 1 0 4 3 0 2 1 0 1 1 2 0 2 0 2 2 2 20
2014 0 4 3 1 1 3 1 4 2 0 4 2 1 1 2 3 0 1 1 1 1 2 3 1 4 20
2015 2 1 1 3 4 2 2 2 0 0 1 3 4 2 3 1 2 3 1 0 2 0 0 2 2 22
2016 2 2 2 3 3 2 0 2 4 3 5 2 2 4 3 4 2 5 2 1 1 2 2 3 2 20
2017 2 7 5 3 3 3 4 6 0 2 1 2 1 3 2 2 0 7 1 3 3 2 3 1 5 17
使用cut
:
with(raw,table(Year,
cut(Score,breaks=seq(75,100,by=5),
right=TRUE,include.lowest=TRUE)))
(我认为:您可能需要对 right
和 include.lowest
稍作小题大做)
PS with()
并不是必须的,它只是让我们不要在代码中重复 raw$
两次 ...
希望以下内容能帮助您走上正轨!
我首先创建了一个示例数据框,希望它能与您当前使用的相匹配!查看 How to make a great R reproducible example? 以获取有关如何在未来写出真正好的问题的一些指导!
library(dplyr)
# create an example data frame
set.seed(123)
raw <- data.frame(Year = rep(2006:2017,10),
Score= rep(rnorm(12, mean = 80, sd = 10), 10))
head(raw)
#> Year Score
#> 1 2006 74.39524
#> 2 2007 77.69823
#> 3 2008 95.58708
#> 4 2009 80.70508
#> 5 2010 81.29288
#> 6 2011 97.15065
# create a new "group" column and assign each row into a group based on score
raw <- raw %>%
mutate(group = if_else(Score < 75, "<75",
if_else(Score >= 75 & Score < 80, "75-80",
if_else(Score >= 80 & Score < 85, "80-85",
if_else(Score >= 85 & Score < 90, "85-90",
if_else(Score >= 90 & Score < 95, "90-95",
if_else(Score >= 95 & Score <= 100, "95-100", "error")))))))
head(raw)
#> Year Score group
#> 1 2006 74.39524 <75
#> 2 2007 77.69823 75-80
#> 3 2008 95.58708 95-100
#> 4 2009 80.70508 80-85
#> 5 2010 81.29288 80-85
#> 6 2011 97.15065 95-100
# summarise counts by year
raw %>%
group_by(group) %>%
summarise(n = n())
#> # A tibble: 5 x 2
#> group n
#> <chr> <int>
#> 1 <75 30
#> 2 75-80 20
#> 3 80-85 40
#> 4 90-95 10
#> 5 95-100 20
我有一组数据,按年份 (raw$Year)
显示测试分数 (raw$Score)
。
我想创建一个 table,在其中我将年份视为行,将分数视为列,并将分数以 5 为增量进行分组。
当我 运行table(raw$Year,raw$Score)
时,我得到以下信息。我将如何修改 table(raw$Year,raw$Score)
以使计数按 75-80、81-85、86-90、91、-95 和 96-100 分组?
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
2006 0 1 0 0 1 3 1 0 1 1 1 1 0 0 1 0 0 0 2 1 0 2 0 1 1 6
2007 0 0 1 0 1 1 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 4 2 2 1 5
2008 0 1 2 0 2 2 1 2 1 0 0 0 0 0 0 1 2 0 0 0 1 0 2 3 2 8
2009 1 1 3 0 1 1 6 0 2 1 1 3 1 0 0 2 0 1 0 0 0 1 2 2 1 13
2010 2 5 0 2 3 1 1 3 2 1 1 0 2 1 2 2 1 0 0 2 2 2 2 2 4 24
2011 2 1 1 0 3 1 0 1 2 0 0 1 2 1 1 2 3 0 2 1 2 0 0 2 3 12
2012 1 1 3 5 4 1 1 1 1 0 2 0 1 1 0 1 0 0 1 2 1 0 3 1 2 24
2013 2 2 3 3 1 2 2 3 1 0 4 3 0 2 1 0 1 1 2 0 2 0 2 2 2 20
2014 0 4 3 1 1 3 1 4 2 0 4 2 1 1 2 3 0 1 1 1 1 2 3 1 4 20
2015 2 1 1 3 4 2 2 2 0 0 1 3 4 2 3 1 2 3 1 0 2 0 0 2 2 22
2016 2 2 2 3 3 2 0 2 4 3 5 2 2 4 3 4 2 5 2 1 1 2 2 3 2 20
2017 2 7 5 3 3 3 4 6 0 2 1 2 1 3 2 2 0 7 1 3 3 2 3 1 5 17
使用cut
:
with(raw,table(Year,
cut(Score,breaks=seq(75,100,by=5),
right=TRUE,include.lowest=TRUE)))
(我认为:您可能需要对 right
和 include.lowest
稍作小题大做)
PS with()
并不是必须的,它只是让我们不要在代码中重复 raw$
两次 ...
希望以下内容能帮助您走上正轨!
我首先创建了一个示例数据框,希望它能与您当前使用的相匹配!查看 How to make a great R reproducible example? 以获取有关如何在未来写出真正好的问题的一些指导!
library(dplyr)
# create an example data frame
set.seed(123)
raw <- data.frame(Year = rep(2006:2017,10),
Score= rep(rnorm(12, mean = 80, sd = 10), 10))
head(raw)
#> Year Score
#> 1 2006 74.39524
#> 2 2007 77.69823
#> 3 2008 95.58708
#> 4 2009 80.70508
#> 5 2010 81.29288
#> 6 2011 97.15065
# create a new "group" column and assign each row into a group based on score
raw <- raw %>%
mutate(group = if_else(Score < 75, "<75",
if_else(Score >= 75 & Score < 80, "75-80",
if_else(Score >= 80 & Score < 85, "80-85",
if_else(Score >= 85 & Score < 90, "85-90",
if_else(Score >= 90 & Score < 95, "90-95",
if_else(Score >= 95 & Score <= 100, "95-100", "error")))))))
head(raw)
#> Year Score group
#> 1 2006 74.39524 <75
#> 2 2007 77.69823 75-80
#> 3 2008 95.58708 95-100
#> 4 2009 80.70508 80-85
#> 5 2010 81.29288 80-85
#> 6 2011 97.15065 95-100
# summarise counts by year
raw %>%
group_by(group) %>%
summarise(n = n())
#> # A tibble: 5 x 2
#> group n
#> <chr> <int>
#> 1 <75 30
#> 2 75-80 20
#> 3 80-85 40
#> 4 90-95 10
#> 5 95-100 20