如何创建一个确定r中变量中是否缺少值的列

发布于 2025-02-09 16:47:03 字数 1434 浏览 1 评论 0原文

我正在尝试确定基于max.score的列是否缺少数字类别。这是一个示例数据集。

    df <- data.frame(id = c(1,1,1,1,1, 2,2,2,2,2, 3,3,3,3,3),
                 score = c(0,0,2,0,2, 0,1,1,0,1, 0,1,0,1,0),
                 max.score = c(2,2,2,2,2, 1,1,1,1,1, 2,2,2,2,2))
    
   > df
   id score max.score
1   1     0         2
2   1     0         2
3   1     2         2
4   1     0         2
5   1     2         2
6   2     0         1
7   2     1         1
8   2     1         1
9   2     0         1
10  2     1         1
11  3     0         2
12  3     1         2
13  3     0         2
14  3     1         2
15  3     0         2

对于ID = 1,基于max.score,它缺少类别1。我想添加缺少列,说1。当ID = 3缺少Score> = = 2 ,缺失列应表示2的值。如果缺少多个类别,则将指示这些丢失的类别为例如1,3。所需的输出应该是:

> df
   id score max.score  missing
1   1     0         2    1
2   1     0         2    1
3   1     2         2    1
4   1     0         2    1
5   1     2         2    1
6   2     0         1    NA
7   2     1         1    NA
8   2     1         1    NA
9   2     0         1    NA
10  2     1         1    NA
11  3     0         2    2
12  3     1         2    2
13  3     0         2    2
14  3     1         2    2
15  3     0         2    2

有什么想法吗? 谢谢!

I am trying to identify if a column has a missing number category based on a max.score. Here is a sample dataset.

    df <- data.frame(id = c(1,1,1,1,1, 2,2,2,2,2, 3,3,3,3,3),
                 score = c(0,0,2,0,2, 0,1,1,0,1, 0,1,0,1,0),
                 max.score = c(2,2,2,2,2, 1,1,1,1,1, 2,2,2,2,2))
    
   > df
   id score max.score
1   1     0         2
2   1     0         2
3   1     2         2
4   1     0         2
5   1     2         2
6   2     0         1
7   2     1         1
8   2     1         1
9   2     0         1
10  2     1         1
11  3     0         2
12  3     1         2
13  3     0         2
14  3     1         2
15  3     0         2

for the id = 1, based on the max.score, it is missing the category 1. I would like to add missing column saying something like 1. When id=3 is missing score = 2, the missing column should indicate a value of 2. If there are more than one category is missing, then it would indicate those missing categories as ,for example, 1,3. The desired output should be:

> df
   id score max.score  missing
1   1     0         2    1
2   1     0         2    1
3   1     2         2    1
4   1     0         2    1
5   1     2         2    1
6   2     0         1    NA
7   2     1         1    NA
8   2     1         1    NA
9   2     0         1    NA
10  2     1         1    NA
11  3     0         2    2
12  3     1         2    2
13  3     0         2    2
14  3     1         2    2
15  3     0         2    2

Any thoughts?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

走走停停 2025-02-16 16:47:03
df %>%
  group_by(id) %>%
  mutate(missing = toString(setdiff(0:max.score[1], unique(score))),
         missing = ifelse(nzchar(missing), missing, NA))

     # A tibble: 15 x 4
# Groups:   id [3]
      id score max.score missing
   <dbl> <dbl>     <dbl> <chr>  
 1     1     0         2 1      
 2     1     0         2 1      
 3     1     2         2 1      
 4     1     0         2 1      
 5     1     2         2 1      
 6     2     0         1 NA     
 7     2     1         1 NA     
 8     2     1         1 NA     
 9     2     0         1 NA     
10     2     1         1 NA     
11     3     0         2 2      
12     3     1         2 2      
13     3     0         2 2      
14     3     1         2 2      
15     3     0         2 2  
df %>%
  group_by(id) %>%
  mutate(missing = toString(setdiff(0:max.score[1], unique(score))),
         missing = ifelse(nzchar(missing), missing, NA))

     # A tibble: 15 x 4
# Groups:   id [3]
      id score max.score missing
   <dbl> <dbl>     <dbl> <chr>  
 1     1     0         2 1      
 2     1     0         2 1      
 3     1     2         2 1      
 4     1     0         2 1      
 5     1     2         2 1      
 6     2     0         1 NA     
 7     2     1         1 NA     
 8     2     1         1 NA     
 9     2     0         1 NA     
10     2     1         1 NA     
11     3     0         2 2      
12     3     1         2 2      
13     3     0         2 2      
14     3     1         2 2      
15     3     0         2 2  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文