如何使用 R 或 Stata 识别数据集中成绩保留的学生?

发布于 2025-01-10 00:17:16 字数 1240 浏览 0 评论 0原文

我有一个小数据集,其中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留蓝 2025-01-17 00:17:16

您可以检查每个组是否包含任何 重复 等级

library(tidyverse)

df %>%
  group_by(id) %>%
  mutate(GR = +any(duplicated(grade)))

输出

      id grade    GR
   <dbl> <chr> <int>
 1  1000 1         0
 2  1000 2         0
 3  1000 3         0
 4  1000 4         0
 5  1000 5         0
 6  1000 6         0
 7  1001 1         1
 8  1001 2         1
 9  1001 3         1
10  1001 4         1
# … with 18 more rows

You can check in each group if contains any duplicated grade:

library(tidyverse)

df %>%
  group_by(id) %>%
  mutate(GR = +any(duplicated(grade)))

Output

      id grade    GR
   <dbl> <chr> <int>
 1  1000 1         0
 2  1000 2         0
 3  1000 3         0
 4  1000 4         0
 5  1000 5         0
 6  1000 6         0
 7  1001 1         1
 8  1001 2         1
 9  1001 3         1
10  1001 4         1
# … with 18 more rows
不知在何时 2025-01-17 00:17:16

如果您也想要 Stata 解决方案,那么这个问题就不会以友好的方式提出,但这是一种方法。这个想法只是获取任何重复成绩的指示符,然后将其传播到该标识符的其他观察(记录、行)。

* Example generated by -dataex-. For more info, type help dataex
clear
input int id byte grade float GR
1000 1 0
1000 2 0
1000 3 0
1000 4 0
1000 5 0
1000 6 0
1001 1 1
1001 2 1
1001 3 1
1001 4 1
1001 5 1
1001 5 1
1001 6 1
1002 1 1
1002 2 1
1002 2 1
1002 3 1
1002 4 1
1002 4 1
1002 4 1
1002 5 1
1002 6 1
1003 1 0
1003 2 0
1003 3 0
1003 4 0
1003 5 0
1003 6 0
end

bysort id grade : gen wanted = _N > 1

bysort id (wanted) : replace wanted = wanted[_N]

sort id grade

list, sepby(id)

     +----------------------------+
     |   id   grade   GR   wanted |
     |----------------------------|
  1. | 1000       1    0        0 |
  2. | 1000       2    0        0 |
  3. | 1000       3    0        0 |
  4. | 1000       4    0        0 |
  5. | 1000       5    0        0 |
  6. | 1000       6    0        0 |
     |----------------------------|
  7. | 1001       1    1        1 |
  8. | 1001       2    1        1 |
  9. | 1001       3    1        1 |
 10. | 1001       4    1        1 |
 11. | 1001       5    1        1 |
 12. | 1001       5    1        1 |
 13. | 1001       6    1        1 |
     |----------------------------|
 14. | 1002       1    1        1 |
 15. | 1002       2    1        1 |
 16. | 1002       2    1        1 |
 17. | 1002       3    1        1 |
 18. | 1002       4    1        1 |
 19. | 1002       4    1        1 |
 20. | 1002       4    1        1 |
 21. | 1002       5    1        1 |
 22. | 1002       6    1        1 |
     |----------------------------|
 23. | 1003       1    0        0 |
 24. | 1003       2    0        0 |
 25. | 1003       3    0        0 |
 26. | 1003       4    0        0 |
 27. | 1003       5    0        0 |
 28. | 1003       6    0        0 |
     +----------------------------+

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.

* Example generated by -dataex-. For more info, type help dataex
clear
input int id byte grade float GR
1000 1 0
1000 2 0
1000 3 0
1000 4 0
1000 5 0
1000 6 0
1001 1 1
1001 2 1
1001 3 1
1001 4 1
1001 5 1
1001 5 1
1001 6 1
1002 1 1
1002 2 1
1002 2 1
1002 3 1
1002 4 1
1002 4 1
1002 4 1
1002 5 1
1002 6 1
1003 1 0
1003 2 0
1003 3 0
1003 4 0
1003 5 0
1003 6 0
end

bysort id grade : gen wanted = _N > 1

bysort id (wanted) : replace wanted = wanted[_N]

sort id grade

list, sepby(id)

     +----------------------------+
     |   id   grade   GR   wanted |
     |----------------------------|
  1. | 1000       1    0        0 |
  2. | 1000       2    0        0 |
  3. | 1000       3    0        0 |
  4. | 1000       4    0        0 |
  5. | 1000       5    0        0 |
  6. | 1000       6    0        0 |
     |----------------------------|
  7. | 1001       1    1        1 |
  8. | 1001       2    1        1 |
  9. | 1001       3    1        1 |
 10. | 1001       4    1        1 |
 11. | 1001       5    1        1 |
 12. | 1001       5    1        1 |
 13. | 1001       6    1        1 |
     |----------------------------|
 14. | 1002       1    1        1 |
 15. | 1002       2    1        1 |
 16. | 1002       2    1        1 |
 17. | 1002       3    1        1 |
 18. | 1002       4    1        1 |
 19. | 1002       4    1        1 |
 20. | 1002       4    1        1 |
 21. | 1002       5    1        1 |
 22. | 1002       6    1        1 |
     |----------------------------|
 23. | 1003       1    0        0 |
 24. | 1003       2    0        0 |
 25. | 1003       3    0        0 |
 26. | 1003       4    0        0 |
 27. | 1003       5    0        0 |
 28. | 1003       6    0        0 |
     +----------------------------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文