-1

I have some data and I found some outliers to revise. But instead of replace all values in that column, I want to use certain specific criteria to replace the outliers in that column.

For example, in column day, I want to replace 80 to 8, 70 to 7 for the participant who has UniqueKey == 1234 and UniqueKey == 1321.

And for participant whose UniqueKey == 1484, I want to replace column month value from 79 to 09, and column day to 7.

Like that replace with multiple conditions, how can I achieve that?

Thanks a lot.

This is my code, it does not work at all.

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") )
              )
Serkan
  • 1,855
  • 6
  • 20
  • Please `dput(head(.))` your data! – Serkan Aug 16 '21 at 20:27
  • Does this answer your question? [Using dplyr to conditionally replace values in a column](https://stackoverflow.com/questions/35610437/using-dplyr-to-conditionally-replace-values-in-a-column) – mikebader Aug 16 '21 at 20:31
  • Run dput(head(.)), shows: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")... –  Aug 16 '21 at 20:33
  • @mikebader I looked the links, still dont know how to use multiple condition to replace specific value. Can you make an example of my case? thx~~! –  Aug 16 '21 at 20:45
  • Where are the `day` and `month` columns in your dataframe? And do you mean & or | when you state UniqueKey == 1234 and UniqueKey == 1321. I guess it should be or? – TarJae Aug 16 '21 at 20:50
  • @TarJae : I separate column "Enterdateofexam2", which was dd.mm format , like"7.02","11.03","23.01"... –  Aug 16 '21 at 20:52
  • That output of `dput` is incomplete. You've asked several questions over the past few days with seemingly the same dataset (at least the same name) without making the [reproducible examples](https://stackoverflow.com/q/5963269/5325862) that will make it much easier to help – camille Aug 16 '21 at 21:33

1 Answers1

3

Update: thank to valuable comment of Onyambu:

jimma3n<-jimma3 %>%
    select(Enterdateofexam,Enterdateofexam2, Enterdayofexam, UniqueKey,MEDICALRECORD)%>%
    mutate(
        day= case_when(UniqueKey == 1234 | UniqueKey= 1321 & day==80 ~ 8,
                       UniqueKey == 1234 | UniqueKey= 1321 & day==70 ~ 7,
                       UniqueKey == 1484 & day==79 ~ 7,
                       TRUE ~ day),
        month = case_when(UniqueKey== 1484 & month==79 ~ 09,
                       TRUE ~ month)
        )

OK try this and tell me please:

jimma3n<-jimma3 %>%
    select(Enterdateofexam,Enterdateofexam2, Enterdayofexam, UniqueKey,MEDICALRECORD)%>%
    mutate(
        day= case_when(UniqueKey == 1234 | UniqueKey= 1321 & day==80 ~ 8,
                       UniqueKey == 1234 | UniqueKey= 1321 & day==70 ~ 7,
                       UniqueKey == 1484 & day==79 ~ 7,
                       TRUE ~ day)
        ) %>%
    mutate(
        month = case_when(UniqueKey== 1484 & month==79 ~ 09,
        TRUE ~ month)
        )
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 1
    why have two mutate functions? +1 though – Onyambu Aug 16 '21 at 21:03
  • 1
    I just took the code from op. I was not sure because of `day` and `month`. So it was not possible for me to test. Do you think we could combine both in one mutate? – TarJae Aug 16 '21 at 21:06
  • 2
    yes why not. mutate can do multiple assignments. NOt just one column. – Onyambu Aug 16 '21 at 21:08
  • Thanks @TarJae, it only has a tiny issue when I run it, saying "Error: Problem with `mutate()` input `day`. x must be a double vector, not a character vector. ℹ Input `day` is `case_when(...)`. " I do not know what this means....Before that, I used as. character(day), as. character(month) to convert my day and month as character. –  Aug 16 '21 at 21:15
  • 1
    I will check and update. just a second. it is no problem. but i guess it is the same with month. So please do `str(jimma3)` and post the output from your console. – TarJae Aug 16 '21 at 21:25
  • 1
    Replace`~8 and ~7 and ~7` in `mutate(day...` with `~'8' and ~'7' and ~'7'`. If the error occurs with month also then do same there. Another possibility is to use `type.convert(as.is = TRUE` as first line befor `mutate` then you may leave all as it is. – TarJae Aug 16 '21 at 21:48
  • Thanks a lot, @TarJae, i solved that tiny issue. You are right, there was format issue and after I changed according to your suggestion, it was gone. Many thanks! –  Aug 17 '21 at 14:25