英文:
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 <- 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)
)
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 %>%
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
答案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('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
Note that I designed different data below to match all the criteria.
Data:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论