-
我有包括学生
mid-terms
和final
考试成绩的数据。 -
该数据以
wide
格式排列,其中每一行对应于显示为SUID
的唯一学生ID。 -
我的数据还包括关于教师的信息,由
TUserId
显示。每个教师可以有多个学生,因此有一个跨几行的通用教师ID。 -
我很想知道是否有老师在学期中期给学生打分相似(如
mid_sum
所示(的情况,在期末考试中给学生打分不一致(如final_sum
所示(。为了记录这种不一致性,我想添加一列Status
来记录这种不匹配或不一致性。
输入:
我的数据df
看起来像这样::
TUserId SUID mid_sum final_sum
115 201 7 1
115 309 8 2
115 404 9 1
209 245 10 2
209 398 10 2
209 510 10 2
209 602 10 1
371 111 11 2
371 115 11 2
371 123 11 3
371 124 11 2
输出:
对于我的输出,我需要这样的东西::
TUserId SUID mid_sum final_sum Status
115 201 7 1 consistent
115 309 8 2 consistent
115 404 9 1 inconsistent
209 245 10 2 consistent
209 398 10 2 consistent
209 510 10 2 consistent
209 602 10 1 inconsistent
371 111 11 2 consistent
371 115 11 2 consistent
371 123 11 3 inconsistent
371 124 11 2 consistent
要求:
我的要求如下:
中期分数要求:
1-当学生的期中成绩较低时,他们的期末成绩不能更高(相对于彼此(。例如,当学生SUID = 309
的期中成绩低于学生SUID = 404
时,他们的期末成绩更高。在这种情况下,我想将SUID = 404
标记为inconsistent
。
2-期中成绩相似的学生也不能有不同的期末成绩。例如,当学生SUID = 602
的期中成绩与教师TUserId = 209
的其他学生相等时,他们的期末成绩较低。同样,当学生SUID = 123
的期中成绩与教师TUserId = 371
的其他学生相等时,他们的期末成绩更高。
最终分数要求:
1-然而,相同的final
分数可以分配给不同期中成绩的学生。我意识到这个要求有点令人困惑。只要期中成绩保持不变或有所提高,期末成绩就可以保持不变。但情况并非如此,即如果该教师的期中成绩开始下降,那么最终成绩就不能保持不变。
2-此外,如果期中分数增加,最终分数也可以增加(或与之前的值保持不变(。
数据导入dput((
数据帧的dput()
如下:
dput(df)
structure(list(
TUserId = c(115L, 115L, 115L, 209L, 209L, 209L, 209L, 371L, 371L, 371L, 371L),
SUID = c(201L, 309L, 404L, 245L, 398L, 510L, 602L, 111L, 115L, 123L, 124L),
mid_sum = c(7L, 8L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L),
final_sum = c(1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 3L, 2L)),
class = "data.frame", row.names = c(NA, -11L))
注意:
我将学生的
mid_sum
和final_sum
分数按升序排序。我只想找出分数分配不一致的情况。从实现的角度来看,总是与以前的值进行比较。
我重新发布了我的问题,因为上一个例子没有澄清我的确切要求识别数据序列基于其他列UserID更改的情况。
部分解决方案:
以下解决方案部分满足了我的要求,但没有涵盖中期成绩相似的学生最终成绩更高的情况。
library(dplyr)
df %>%
arrange(TUserId, mid_sum) %>%
group_by(TUserId) %>%
mutate(
Status = if_else(
sign(final_sum - lag(final_sum, default = 0) + lead(final_sum, default = 0))
== sign(mid_sum - lag(mid_sum, default = 0) + lead(mid_sum, default = 0)),
"consisent", "inconsistent"
)
)
很酷的问题。你的问题解释得很好。
考虑这个代码:
## Rule 1
# we sort by mid sum first, then final sum
# if the cumululative max of the final sum is higher than the current finalsum,
# the mid sum had to be lower
df <- df %>% arrange(mid_sum,final_sum) %>% mutate(inconsistentRule1 = cummax(final_sum)>final_sum)
# Rule 2
# This is a shot in the dark as the inconsitency criteria is a bit fuzzy
# (What if a teacher with only two students on same mid_level assigns different grades,
# which student is to be considered "inconsistent"? The lower or the higher graded)
# i just used the median, in this case students that deviate from the norm
# are considered the inconsistent ones, works with your example
df <- df %>% group_by(TUserId,mid_sum) %>% mutate(inconsistentRule2= final_sum != median(final_sum))
# combine the rules
df <- df %>% ungroup() %>%
mutate(Status=ifelse(
inconsistentRule1 | inconsistentRule2,
"inconsistent",
"consistent"))
# put in order and delete working columns
df %>% arrange(TUserId,SUID) %>%
select(-c("inconsistentRule1","inconsistentRule2"))
结果是您想要的表格