Changing NA to Zero

I am trying to change specific column with NA to Zero

using the following code

Covid_19_Vaccination[4:31][is.na(Covid_19_Vaccination[4:31])]<-0

but it is showing me the following error

Error in [<-:
! Assigned data 0 must be compatible with existing data.
:information_source: Error occurred for column above_60_y_dose2female.
Caused by error in vec_assign():
! Can't convert to .
Backtrace:

  1. base::[<-(*tmp*, is.na(Covid_19_Vaccination[4:31]), value = <dbl>)
  2. tibble:::[<-.tbl_df(...)
  3. tibble:::tbl_subassign_matrix(x, j, value, j_arg, substitute(value))
  4. tibble:::vectbl_assign(x[[j]], cells[[j]], value)
  5. vctrs::vec_assign(x, i, value)

is there way of fix this ?
thanks

Hi @Selemani_Ngwira, welcome!

For get a better help try to put a reproducible example of the data and script.

See a tentative example for change specific columns with NAs to Zero.

Covid_19_Vaccination <- data.frame(
  Country = c("A", "B", "C"),
  Col1 = c(10, NA, 20),
  Col2 = c(30, 40, NA),
  Col3 = c(NA, 50, 60))

Covid_19_Vaccination <- Covid_19_Vaccination %>%
  mutate(across(1:2, ~ifelse(is.na(.), 0, .))) # Select 1 and 2 columns

#  Country Col1 Col2 Col3
#1       A   10   30   NA
#2       B    0   40   50
#3       C   20   NA   60

@M_AcostaCH ,
thanks a lot. it has work

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.