Dr Eberhard W Lisse
2021-Oct-25 11:25 UTC
[R] cleanup/replacing a value on condition of another value
Hi, I have data from JHU via the 'coronavirus' package which has a value for the confirmed cases for 2021-10-23 which differs drastically (357) from what is reported in country (23). # A tibble: 962 ? 4 country date type cases <chr> <date> <chr> <int> 1 Namibia 2021-10-24 confirmed 23 2 Namibia 2021-10-24 death 4 3 Namibia 2021-10-23 confirmed 357 4 Namibia 2021-10-23 death 1 5 Namibia 2021-10-22 confirmed 30 6 Namibia 2021-10-22 death 1 # ? with 956 more rows I am using a '%>%' pipeline and am struggling to mutate 'cases' to NA using something like country == 'Namibia' & date == '2021-10-23' & cases == 357 so that if or when the data-set is corrected I don't have to change the code (immediately), even after some googling. I can do cases == 357 only, but that could find other cases as well, which is obviously not the thing to do Any suggestions? greetings, el
Eric Berger
2021-Oct-25 11:31 UTC
[R] cleanup/replacing a value on condition of another value
The tibble shows the 'date' column as type date but you are comparing
io a string.
Perhaps replace that piece by
date == as.Date("2021-10-23")
Not tested.
HTH,
Eric
On Mon, Oct 25, 2021 at 2:26 PM Dr Eberhard W Lisse <nospam at lisse.na>
wrote:>
> Hi,
>
> I have data from JHU via the 'coronavirus' package which has a
value for
> the confirmed cases for 2021-10-23 which differs drastically (357) from
> what is reported in country (23).
>
> # A tibble: 962 ? 4
> country date type cases
> <chr> <date> <chr> <int>
> 1 Namibia 2021-10-24 confirmed 23
> 2 Namibia 2021-10-24 death 4
> 3 Namibia 2021-10-23 confirmed 357
> 4 Namibia 2021-10-23 death 1
> 5 Namibia 2021-10-22 confirmed 30
> 6 Namibia 2021-10-22 death 1
> # ? with 956 more rows
>
> I am using a '%>%' pipeline and am struggling to mutate
'cases' to NA
> using something like
>
> country == 'Namibia' & date == '2021-10-23'
& cases == 357
>
> so that if or when the data-set is corrected I don't have to change the
> code (immediately), even after some googling.
>
> I can do
>
> cases == 357
>
> only, but that could find other cases as well, which is obviously not
> the thing to do
>
> Any suggestions?
>
> greetings, el
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Rui Barradas
2021-Oct-25 15:26 UTC
[R] cleanup/replacing a value on condition of another value
Hello,
The following works with me.
library(coronavirus)
library(dplyr)
data(coronavirus, package = "coronavirus")
#update_dataset(silence = FALSE)
coronavirus %>%
select(country, date, type, cases) %>%
filter(
country == 'Namibia',
date == '2021-10-23',
cases == 357
)
Can you post the pipe code you are running?
Hope this helps,
Rui Barradas
?s 12:25 de 25/10/21, Dr Eberhard W Lisse escreveu:> Hi,
>
> I have data from JHU via the 'coronavirus' package which has a
value for
> the confirmed cases for 2021-10-23 which differs drastically (357) from
> what is reported in country (23).
>
> ????# A tibble: 962 ? 4
> ????? country date?????? type????? cases
> ????? <chr>?? <date>???? <chr>???? <int>
> ????1 Namibia 2021-10-24 confirmed??? 23
> ????2 Namibia 2021-10-24 death???????? 4
> ????3 Namibia 2021-10-23 confirmed?? 357
> ????4 Namibia 2021-10-23 death???????? 1
> ????5 Namibia 2021-10-22 confirmed??? 30
> ????6 Namibia 2021-10-22 death???????? 1
> ????# ? with 956 more rows
>
> I am using a '%>%' pipeline and am struggling to mutate
'cases' to NA
> using something like
>
> ????country == 'Namibia' & date == '2021-10-23' &
cases == 357
>
> so that if or when the data-set is corrected I don't have to change the
> code (immediately), even after some googling.
>
> I can do
>
> ????cases == 357
>
> only, but that could find other cases as well, which is obviously not
> the thing to do
>
> Any suggestions?
>
> greetings, el
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.