R: 发现哪些学生正在提高他们的成绩

huangapple go评论59阅读模式
英文:

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))

&gt; 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 &lt;- function(grades){
  for(i in 2:length(grades)){
    if(grades[i] &lt; grades[i-1]){
      return(FALSE)
    }
  }
  return(TRUE)
}

check_grades_decline &lt;- function(grades){
  for(i in 2:length(grades)){
    if(grades[i] &gt; grades[i-1]){
      return(FALSE)
    }
  }
  return(TRUE)
}

Then, I tried to apply these functions to my dataset:

  improving_students &lt;- my_data %&gt;% group_by(id) %&gt;% 
  filter(check_grades_improvement(grade)) %&gt;% 
  select(id) %&gt;% 
  unique()


worse_students &lt;- my_data %&gt;% 
  group_by(id) %&gt;% 
  filter(check_grades_decline(grade)) %&gt;% 
  select(id) %&gt;% 
  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 %&gt;% 
  group_by(id) %&gt;% 
  mutate(x = grade-lag(grade, default = grade[1])) %&gt;% 
  mutate(peformance = case_when(x == 0 ~ &quot;kept_same&quot;,
                                x &gt; 0 ~ &quot;improved&quot;,
                                x &lt; 0 ~ &quot;got_worse&quot;,
                                TRUE ~ NA_character_), .keep=&quot;unused&quot;)
     id  year grade peformance
   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt;     
 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 &lt;- function(grades){
  for(i in 2:length(grades)){
    if(grades[i] &lt; grades[i-1]){
      return(FALSE)
      break
    }
  }
  return(TRUE)
}

check_grades_decline &lt;- function(grades){
  for(i in 2:length(grades)){
    if(grades[i] &gt; grades[i-1]){
      return(FALSE)
      break
    }
  }
  return(TRUE)
}

-测试

 my_data %&gt;%
   group_by(id) %&gt;%
   filter(check_grades_improvement(grade)) %&gt;%
   ungroup %&gt;%
   select(id) %&gt;%
   unique()
# A tibble: 1 &#215; 1
     id
  &lt;dbl&gt;
1     1


my_data %&gt;%
   group_by(id) %&gt;%
   filter(check_grades_decline(grade)) %&gt;
   ungroup() %&gt;
   select(id) %&gt; 
   unique()
# A tibble: 1 &#215; 1
     id
  &lt;dbl&gt;
1     2

或者如果是对所有情况

my_data %&gt;%
  arrange(id, year) %&gt;%
  group_by(id) %&gt;%
  filter(c(FALSE, diff(grade) &gt; 0)) %&gt;%
  ungroup %&gt;%
  select(id) %&gt;%
  unique
# A tibble: 2 &#215; 1
     id
  &lt;dbl&gt;
1     1
2     3
my_data %&gt;%
  arrange(id, year) %&gt;%
  group_by(id) %&gt;%
  filter(c(FALSE, diff(grade) &lt; 0)) %&gt;%
  ungroup %&gt;%
  select(id) %&gt;%
  unique
# A tibble: 2 &#215; 1
     id
  &lt;dbl&gt;
1     2
2     3
英文:

If we want to break the function at the first instance

check_grades_improvement &lt;- function(grades){
  for(i in 2:length(grades)){
    if(grades[i] &lt; grades[i-1]){
      return(FALSE)
      break
    }
  }
  return(TRUE)
}

check_grades_decline &lt;- function(grades){
  for(i in 2:length(grades)){
    if(grades[i] &gt; grades[i-1]){
      return(FALSE)
      break
    }
  }
  return(TRUE)
}

-testing

 my_data %&gt;%
   group_by(id) %&gt;% 
   filter(check_grades_improvement(grade)) %&gt;% 
   ungroup %&gt;%
   select(id) %&gt;%
   unique()
# A tibble: 1 &#215; 1
     id
  &lt;dbl&gt;
1     1


my_data %&gt;% 
   group_by(id) %&gt;% 
   filter(check_grades_decline(grade)) %&gt;%
   ungroup() %&gt;%
   select(id) %&gt;% 
   unique()
# A tibble: 1 &#215; 1
     id
  &lt;dbl&gt;
1     2


Or if it is for all instances

my_data %&gt;% 
  arrange(id, year) %&gt;%
  group_by(id) %&gt;% 
  filter(c(FALSE, diff(grade) &gt; 0)) %&gt;%
  ungroup %&gt;%
  select(id) %&gt;%
  unique
# A tibble: 2 &#215; 1
     id
  &lt;dbl&gt;
1     1
2     3
my_data %&gt;% 
  arrange(id, year) %&gt;% 
  group_by(id) %&gt;% 
  filter(c(FALSE, diff(grade) &lt; 0)) %&gt;%
  ungroup %&gt;%
  select(id) %&gt;%
  unique
# A tibble: 2 &#215; 1
     id
  &lt;dbl&gt;
1     2
2     3

</details>



huangapple
  • 本文由 发表于 2023年2月6日 12:29:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定