以下是我拥有的一些数据的示例
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年,其中一些人失踪了.
我想在遗漏身份证的Number
栏中加0
.
我的真实数据有25个位置,所以理想情况下,我希望能够抓住ID的完整列表,并对照每个位置判断,并在缺少的地方添加0.
我试过tidyverse::mutate
个case_when
个,但我没有任何进展.感谢任何帮助.