如何使用 R 或 Stata 识别数据集中成绩保留的学生?
我有一个小数据集,其中有 4 条关于学生学业进展的数据记录。 第一列是id,第二列是等级。我的目标是创建一个名为“GR”(成绩保留)的新变量来识别成绩保留的学生。如果是,则GR=1,否则GR=0。生成的数据集应如下所示,
structure(list(id = c(1000, 1000, 1000, 1000, 1000, 1000, 1001,
1001, 1001, 1001, 1001, 1001, 1001, 1002, 1002, 1002, 1002, 1002,
1002, 1002, 1002, 1002, 1003, 1003, 1003, 1003, 1003, 1003),
grade = c("1", "2", "3", "4", "5", "6", "1", "2", "3", "4",
"5", "5", "6", "1", "2", "2", "3", "4", "4", "4", "5", "6",
"1", "2", "3", "4", "5", "6"), GR= c("0", "0",
"0", "0", "0", "0", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "0", "0", "0", "0",
"0", "0")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-28L))->df_new
原始数据集的结构如下,
structure(list(id = c(1000, 1000, 1000, 1000, 1000, 1000, 1001,
1001, 1001, 1001, 1001, 1001, 1001, 1002, 1002, 1002, 1002, 1002,
1002, 1002, 1002, 1002, 1003, 1003, 1003, 1003, 1003, 1003),
grade = c("1", "2", "3", "4", "5", "6", "1", "2", "3", "4",
"5", "5", "6", "1", "2", "2", "3", "4", "4", "4", "5", "6",
"1", "2", "3", "4", "5", "6")), row.names = c(NA, -28L), class = c("tbl_df",
"tbl", "data.frame"))->df
感谢您的帮助或指导!
I have a small dataset in which there are 4 data records about student academic progression.
The fist column is id, and the second column is grade. My goal is to create a new variable called "GR" (Grade Retention) to identify the students with grade retention. If it is the case, then GR=1, otherwise, GR=0. The resulting dataset should look like below,
structure(list(id = c(1000, 1000, 1000, 1000, 1000, 1000, 1001,
1001, 1001, 1001, 1001, 1001, 1001, 1002, 1002, 1002, 1002, 1002,
1002, 1002, 1002, 1002, 1003, 1003, 1003, 1003, 1003, 1003),
grade = c("1", "2", "3", "4", "5", "6", "1", "2", "3", "4",
"5", "5", "6", "1", "2", "2", "3", "4", "4", "4", "5", "6",
"1", "2", "3", "4", "5", "6"), GR= c("0", "0",
"0", "0", "0", "0", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "0", "0", "0", "0",
"0", "0")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-28L))->df_new
The original data set is structured as follows,
structure(list(id = c(1000, 1000, 1000, 1000, 1000, 1000, 1001,
1001, 1001, 1001, 1001, 1001, 1001, 1002, 1002, 1002, 1002, 1002,
1002, 1002, 1002, 1002, 1003, 1003, 1003, 1003, 1003, 1003),
grade = c("1", "2", "3", "4", "5", "6", "1", "2", "3", "4",
"5", "5", "6", "1", "2", "2", "3", "4", "4", "4", "5", "6",
"1", "2", "3", "4", "5", "6")), row.names = c(NA, -28L), class = c("tbl_df",
"tbl", "data.frame"))->df
Appreciate your kindly help or guidance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检查每个组是否包含
任何
重复
等级
:输出
You can check in each group if contains
any
duplicated
grade
:Output
如果您也想要 Stata 解决方案,那么这个问题就不会以友好的方式提出,但这是一种方法。这个想法只是获取任何重复成绩的指示符,然后将其传播到该标识符的其他观察(记录、行)。
The question isn't posed in a friendly way if you want a Stata solution too, but here is one way to do it. The idea is just to get an indicator for any repeated grade and then to spread it to other observations (records, rows) for that identifier.