【发布时间】:2021-10-18 20:34:12
【问题描述】:
我有一些data,我发现了一些异常值需要修改。但我不想替换该列中的所有值,而是想使用某些特定标准来替换该列中的异常值。
例如,在day 列中,对于拥有UniqueKey == 1234 和UniqueKey == 1321 的参与者,我想将80 替换为8,70 替换为7。
对于UniqueKey == 1484 的参与者,我想将month 列的值从79 替换为09,并将day 列替换为7。
像这样用多个条件替换,我该如何实现?
非常感谢。
这是我的代码,它根本不起作用。
jimma3n<-jimma3 %>%
select(Enterdateofexam,Enterdateofexam2, Enterdayofexam, UniqueKey,MEDICALRECORD)%>%
mutate(
day=str_replace_all(day,c("80"="8","70"="7")),
month=str_replace(month,"25"="05"),
if_else((UniqueKey=="1484" & month=="79"), (day=="7"& month=="09") )
)
【问题讨论】:
-
请
dput(head(.))您的数据! -
运行 dput(head(.)), 显示:structure(list(UniqueKey = c("530", "530", "530", "531", "531", "531" ), MEDICALRECORD = c("577207", "577207", "577207", "575333", "575333", "575333"),Enterdateofexam = c("7.06", "8.06", "9.06", "22.12" , "23.12", "24.12")...
-
@mikebader 我查看了链接,仍然不知道如何使用多个条件来替换特定值。你能举一个我的例子吗?谢谢~~!
-
数据框中的
day和month列在哪里?你的意思是 & 还是 |当您声明 UniqueKey == 1234 和 UniqueKey == 1321 时。我想应该是 or?