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.
Error occurred for column above_60_y_dose2female
.
Caused by error in vec_assign()
:
! Can't convert to .
Backtrace:
base::[<-
(*tmp*
, is.na(Covid_19_Vaccination[4:31]), value = <dbl>
)
tibble:::[<-.tbl_df
(...)
tibble:::tbl_subassign_matrix(x, j, value, j_arg, substitute(value))
tibble:::vectbl_assign(x[[j]], cells[[j]], value)
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.
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
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
system
Closed
March 11, 2024, 8:37pm
4
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.