基于行的标准确定学生的等级

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

Determining student grade based on criteria rowwise

问题

我想确定一个学生是“PASSED”还是“FAILED”,我们需要应用以下标准:

如果学生的总分大于200分,并且在所有科目中都得分超过33分,那么学生被认为“PASSED”。

如果学生在1或2个科目中得分低于33分,总分大于200分,那么学生获得“ER”(重修资格)。

如果学生在超过2个科目中得分低于33分,或者总分小于等于200分,那么学生被认为“FAILED”。


df <- data.frame(
  student = c("a", "b", "c", "d", "e"),
  subject1 = c(40, 50, 60, 70, 80),   
  subject2 = c(35, 45, 55, 65, 75),  
  subject3 = c(38, 48, 58, 68, 78),   
  subject4 = c(33, 34, 35, 36, 37),  
  subject5 = c(42, 52, 62, 72, 82) 
)
英文:

I would like to find out whether a student is “PASSED” or “FAILED,” we have to apply the following criteria:

If the student has scored greater than 200 as total marks and scored greater than 33 in all subjects, then the student is “PASSED.”

If a student has scored less than 33 in 1 or 2 subjects and total marks are greater than 200, the student has got “ER” (Essential Repeat).

If the student has scored less than 33 in more than 2 subjects or less than or equal to 200 as total marks, then the student is “FAILED.”


df &lt;- data.frame(
  student = c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;),
  subject1 = c(40, 50, 60, 70, 80),   
  subject2 = c(35, 45, 55, 65, 75),  
  subject3 = c(38, 48, 58, 68, 78),   
  subject4 = c(33, 34, 35, 36, 37),  
  subject5 = c(42, 52, 62, 72, 82) 
)

Any help?

答案1

得分: 0

你可以这样做:

    library(tidyverse)
    df %>%
      mutate(test = apply(across(-student),
                          1,
                          function(x) case_when(sum(x) > 200 & sum(x > 33) == 5 ~ 'PASSED',
                                                sum(x) > 200 & sum(x < 33) %in% c(1, 2) ~ 'ER',
                                                TRUE ~ 'FAILED')))

      student subject1 subject2 subject3 subject4 subject5   test
    1       a       40       35       38       33       42 FAILED
    2       b       50       45       48       34       52 PASSED
    3       c       60       55       58       35       62 PASSED
    4       d       70       65       68       36       72 PASSED
    5       e       80       75       78       37       82 PASSED
英文:

You can do:

library(tidyverse)
df %&gt;%
  mutate(test = apply(across(-student),
                      1, 
                      function(x) case_when(sum(x) &gt; 200 &amp; sum(x &gt; 33) == 5 ~ &#39;PASSED&#39;,
                                            sum(x) &gt; 200 &amp; sum(x &lt; 33) %in% c(1, 2) ~ &#39;ER&#39;,
                                            TRUE ~ &#39;FAILED&#39;)))

  student subject1 subject2 subject3 subject4 subject5   test
1       a       40       35       38       33       42 FAILED
2       b       50       45       48       34       52 PASSED
3       c       60       55       58       35       62 PASSED
4       d       70       65       68       36       72 PASSED
5       e       80       75       78       37       82 PASSED

答案2

得分: 0

比较 rowSums 与相应的成就并将 + 1 添加到布尔值。逐渐使用 replace 进行附加条件。给出 1, 1, 2, 3, 2,允许您对 c('FAILED', 'PASSED', 'ER') 向量进行子集化选择。

df$achv <- c('FAILED', 'PASSED', 'ER')[(1 + (rowSums(df
展开收缩
< 33) <= 2)) | replace(rowSums(df
展开收缩
) <= 200, 1) |
replace(rowSums(df
展开收缩
) > 200 & rowSums(df
展开收缩
< 33) %in% 1:2, 3)]
df # student subject1 subject2 subject3 subject4 subject5 achv # 1 a 0 0 0 33 42 FAILED # 2 b 50 0 48 34 0 FAILED # 3 c 60 55 58 35 34 PASSED # 4 d 70 65 68 36 0 ER # 5 e 80 75 78 37 82 PASSED

请注意,我设计了不同的数据以匹配所有条件。

数据:

df <- structure(list(student = c("a", "b", "c", "d", "e"), subject1 = c(0L, 
50L, 60L, 70L, 80L), subject2 = c(0L, 0L, 55L, 65L, 75L), subject3 = c(0L, 
48L, 58L, 68L, 78L), subject4 = 33:37, subject5 = c(42L, 0L, 
34L, 0L, 82L), achv = c("FAILED", "FAILED", "PASSED", "ER", "PASSED"
)), row.names = c("1", "2", "3", "4", "5"), class = "data.frame")
英文:

Compare rowSums with the respective achievements and add + 1 to the boolean. replace incrementally for additional criteria. Gives 1, 1, 2, 3, 2 which allows you to subset a vector of c(&#39;FAILED&#39;, &#39;PASSED&#39;, &#39;ER&#39;).

df$achv &lt;- c(&#39;FAILED&#39;, &#39;PASSED&#39;, &#39;ER&#39;)[(1 + (rowSums(df
展开收缩
&lt; 33) &lt;= 2)) |&gt; replace(rowSums(df
展开收缩
) &lt;= 200, 1) |&gt; replace(rowSums(df
展开收缩
) &gt; 200 &amp; rowSums(df
展开收缩
&lt; 33) %in% 1:2, 3)] df # student subject1 subject2 subject3 subject4 subject5 achv # 1 a 0 0 0 33 42 FAILED # 2 b 50 0 48 34 0 FAILED # 3 c 60 55 58 35 34 PASSED # 4 d 70 65 68 36 0 ER # 5 e 80 75 78 37 82 PASSED

Note that I designed different data below to match all the criteria.


Data:

df &lt;- structure(list(student = c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;), subject1 = c(0L, 
50L, 60L, 70L, 80L), subject2 = c(0L, 0L, 55L, 65L, 75L), subject3 = c(0L, 
48L, 58L, 68L, 78L), subject4 = 33:37, subject5 = c(42L, 0L, 
34L, 0L, 82L), achv = c(&quot;FAILED&quot;, &quot;FAILED&quot;, &quot;PASSED&quot;, &quot;ER&quot;, &quot;PASSED&quot;
)), row.names = c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;), class = &quot;data.frame&quot;)

huangapple
  • 本文由 发表于 2023年6月25日 17:22:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76549730.html
匿名

发表评论

匿名网友

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

确定