如何在缺少观测值的地方添加零

问题描述 投票:0回答:1

这是我拥有的一些数据的示例

 dput(df)
structure(list(ID = c("a", "b", "c", "d", "e", "f", "g", "h", 
"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "b", 
"c", "d", "e", "f", "j", "k", "n", "m", "q", "r"), Number = c(1, 
2, 1, 3, 4, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 
1, 2, 1, 1, 1, 1, 1, 2, 2), Location = c(1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2), Surveyor = c("JKK", "JKK", "JKK", "JKK", "JKK", "JKK", 
"JKK", "JKK", "JKK", "JKK", "JKK", "JKK", "JKK", "JKK", "JKK", 
"JKK", "JKK", "JKK", "JKK", "JKK", "JKK", "JKK", "JKK", "JKK", 
"JKK", "JKK", "JKK", "JKK", "JKK", "JKK", "JKK")), row.names = c(NA, 
-31L), spec = structure(list(cols = list(ID = structure(list(), class = c("collector_character", 
"collector")), Number = structure(list(), class = c("collector_double", 
"collector")), Location = structure(list(), class = c("collector_double", 
"collector")), Surveyor = structure(list(), class = c("collector_character", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x00000253510611f0>, class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

在上面的示例中,我有 col

ID
,其中包含字母 a-t (我的真实数据有其他字母代码),在
Location
1 中,所有这些代码都存在。然而在
Location
2 中,其中一些缺失了。

我想将

0
添加到
Number
列中缺少 ID 的位置。

我的真实数据有 25 个位置,因此理想情况下,我希望能够获取完整的 ID 列表,并针对每个位置进行检查,并在缺少的位置添加 0。

我已经尝试过

tidyverse::mutate
case_when
但我没有取得任何进展。任何帮助表示赞赏。

r missing-data
1个回答
0
投票

您可以使用

tidyr::complete()
来实现此目的。
fill
参数允许您设置您创建的列中的值。

df  |> 
    tidyr::complete(
        ID,
        Location, 
        fill = list(Number = 0)
    )

对于新值,输出将具有

0
表示
Number
NA
表示任何未明确设置的列,即
Surveyor

输出:

# A tibble: 40 × 4
   ID    Location Number Surveyor
   <chr>    <dbl>  <dbl> <chr>   
 1 a            1      1 JKK     
 2 a            2      0 NA      
 3 b            1      2 JKK     
 4 b            2      2 JKK     
 5 c            1      1 JKK     
 6 c            2      1 JKK     
 7 d            1      3 JKK     
 8 d            2      1 JKK     
 9 e            1      4 JKK     
10 e            2      2 JKK     
11 f            1      1 JKK     
12 f            2      1 JKK     
13 g            1      1 JKK     
14 g            2      0 NA      
15 h            1      2 JKK     
16 h            2      0 NA      
17 i            1      2 JKK     
18 i            2      0 NA      
19 j            1      2 JKK     
20 j            2      1 JKK     
21 k            1      2 JKK     
22 k            2      1 JKK     
23 l            1      2 JKK     
24 l            2      0 NA      
25 m            1      1 JKK     
26 m            2      1 JKK     
27 n            1      1 JKK     
28 n            2      1 JKK     
29 o            1      1 JKK     
30 o            2      0 NA      
31 p            1      1 JKK     
32 p            2      0 NA      
33 q            1      1 JKK     
34 q            2      2 JKK     
35 r            1      1 JKK     
36 r            2      2 JKK     
37 s            1      1 JKK     
38 s            2      0 NA      
39 t            1      1 JKK     
40 t            2      0 NA      
© www.soinside.com 2019 - 2024. All rights reserved.