英文:
R: Finding Out Which Students are Improving Their Grade
问题
I am working with the R programming language.
Suppose I have the following dataset of student grades:
my_data = data.frame(id = c(1,1,1,1,1,2,2,2,3,3,3,3), year = c(2010,2011,2012,2013, 2014, 2008, 2009, 2010, 2018, 2019, 2020, 2021), grade = c(55, 56, 61, 61, 62, 90,89,89, 67, 87, 51, 65))
我的问题:我想找出哪些学生的成绩在年份之间有所提高(或保持相同的成绩),以及哪些学生的成绩在年份之间下降。
使用“分组窗口函数”的思想,我尝试编写以下函数:
check_grades_improvement <- function(grades){
for(i in 2:length(grades)){
if(grades[i] < grades[i-1]){
return(FALSE)
}
}
return(TRUE)
}
check_grades_decline <- function(grades){
for(i in 2:length(grades)){
if(grades[i] > grades[i-1]){
return(FALSE)
}
}
return(TRUE)
}
然后,我尝试将这些函数应用于我的数据集:
improving_students <- my_data %>%
group_by(id) %>%
filter(check_grades_improvement(grade)) %>%
select(id) %>%
unique()
worse_students <- my_data %>%
group_by(id) %>%
filter(check_grades_decline(grade)) %>%
select(id) %>%
unique()
但我得到了空结果。
有人能否请告诉我我做错了什么,以及如何解决这个问题?
谢谢!
英文:
I am working with the R programming language.
Suppose I have the following dataset of student grades:
my_data = data.frame(id = c(1,1,1,1,1,2,2,2,3,3,3,3), year = c(2010,2011,2012,2013, 2014, 2008, 2009, 2010, 2018, 2019, 2020, 2021), grade = c(55, 56, 61, 61, 62, 90,89,89, 67, 87, 51, 65))
> my_data
id year grade
1 1 2010 55
2 1 2011 56
3 1 2012 61
4 1 2013 61
5 1 2014 62
6 2 2008 90
7 2 2009 89
8 2 2010 89
9 3 2018 67
10 3 2019 87
11 3 2020 51
12 3 2021 65
My Question: I want to find out which students improved their grades (or kept the same grade) from year to year, and which students got worse grades from year to year.
Using the idea of "grouped window functions", I tried to write the following functions :
check_grades_improvement <- function(grades){
for(i in 2:length(grades)){
if(grades[i] < grades[i-1]){
return(FALSE)
}
}
return(TRUE)
}
check_grades_decline <- function(grades){
for(i in 2:length(grades)){
if(grades[i] > grades[i-1]){
return(FALSE)
}
}
return(TRUE)
}
Then, I tried to apply these functions to my dataset:
improving_students <- my_data %>% group_by(id) %>%
filter(check_grades_improvement(grade)) %>%
select(id) %>%
unique()
worse_students <- my_data %>%
group_by(id) %>%
filter(check_grades_decline(grade)) %>%
select(id) %>%
unique()
But I am getting empty results
Can someone please show me what I am doing wrong and how I can fix this?
Thanks!
答案1
得分: 3
类似于以下内容:
library(dplyr)
my_data %>%
group_by(id) %>%
mutate(x = grade-lag(grade, default = grade[1])) %>%
mutate(peformance = case_when(x == 0 ~ "kept_same",
x > 0 ~ "improved",
x < 0 ~ "got_worse",
TRUE ~ NA_character_), .keep="unused")
id year grade peformance
<dbl> <dbl> <dbl> <chr>
1 1 2010 55 kept_same
2 1 2011 56 improved
3 1 2012 61 improved
4 1 2013 61 kept_same
5 1 2014 62 improved
6 2 2008 90 kept_same
7 2 2009 89 got_worse
8 2 2010 89 kept_same
9 3 2018 67 kept_same
10 3 2019 87 improved
11 3 2020 51 got_worse
12 3 2021 65 improved
英文:
Something like this:
library(dplyr)
my_data %>%
group_by(id) %>%
mutate(x = grade-lag(grade, default = grade[1])) %>%
mutate(peformance = case_when(x == 0 ~ "kept_same",
x > 0 ~ "improved",
x < 0 ~ "got_worse",
TRUE ~ NA_character_), .keep="unused")
id year grade peformance
<dbl> <dbl> <dbl> <chr>
1 1 2010 55 kept_same
2 1 2011 56 improved
3 1 2012 61 improved
4 1 2013 61 kept_same
5 1 2014 62 improved
6 2 2008 90 kept_same
7 2 2009 89 got_worse
8 2 2010 89 kept_same
9 3 2018 67 kept_same
10 3 2019 87 improved
11 3 2020 51 got_worse
12 3 2021 65 improved
答案2
得分: 2
如果我们想在第一次出现时break
函数
check_grades_improvement <- function(grades){
for(i in 2:length(grades)){
if(grades[i] < grades[i-1]){
return(FALSE)
break
}
}
return(TRUE)
}
check_grades_decline <- function(grades){
for(i in 2:length(grades)){
if(grades[i] > grades[i-1]){
return(FALSE)
break
}
}
return(TRUE)
}
-测试
my_data %>%
group_by(id) %>%
filter(check_grades_improvement(grade)) %>%
ungroup %>%
select(id) %>%
unique()
# A tibble: 1 × 1
id
<dbl>
1 1
my_data %>%
group_by(id) %>%
filter(check_grades_decline(grade)) %>
ungroup() %>
select(id) %>
unique()
# A tibble: 1 × 1
id
<dbl>
1 2
或者如果是对所有情况
my_data %>%
arrange(id, year) %>%
group_by(id) %>%
filter(c(FALSE, diff(grade) > 0)) %>%
ungroup %>%
select(id) %>%
unique
# A tibble: 2 × 1
id
<dbl>
1 1
2 3
my_data %>%
arrange(id, year) %>%
group_by(id) %>%
filter(c(FALSE, diff(grade) < 0)) %>%
ungroup %>%
select(id) %>%
unique
# A tibble: 2 × 1
id
<dbl>
1 2
2 3
英文:
If we want to break
the function at the first instance
check_grades_improvement <- function(grades){
for(i in 2:length(grades)){
if(grades[i] < grades[i-1]){
return(FALSE)
break
}
}
return(TRUE)
}
check_grades_decline <- function(grades){
for(i in 2:length(grades)){
if(grades[i] > grades[i-1]){
return(FALSE)
break
}
}
return(TRUE)
}
-testing
my_data %>%
group_by(id) %>%
filter(check_grades_improvement(grade)) %>%
ungroup %>%
select(id) %>%
unique()
# A tibble: 1 × 1
id
<dbl>
1 1
my_data %>%
group_by(id) %>%
filter(check_grades_decline(grade)) %>%
ungroup() %>%
select(id) %>%
unique()
# A tibble: 1 × 1
id
<dbl>
1 2
Or if it is for all instances
my_data %>%
arrange(id, year) %>%
group_by(id) %>%
filter(c(FALSE, diff(grade) > 0)) %>%
ungroup %>%
select(id) %>%
unique
# A tibble: 2 × 1
id
<dbl>
1 1
2 3
my_data %>%
arrange(id, year) %>%
group_by(id) %>%
filter(c(FALSE, diff(grade) < 0)) %>%
ungroup %>%
select(id) %>%
unique
# A tibble: 2 × 1
id
<dbl>
1 2
2 3
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论