After merging a dataframe with another im left with random NA’s for the occasional row. I’d like to set these NA’s to 0 so I can perform calculations with them.
Im trying to do this with:
bothbeams.data = within(bothbeams.data, {
bothbeams.data$x.x = ifelse(is.na(bothbeams.data$x.x) == TRUE, 0, bothbeams.data$x.x)
bothbeams.data$x.y = ifelse(is.na(bothbeams.data$x.y) == TRUE, 0, bothbeams.data$x.y)
})
Where $x.x is one column and $x.y is the other of course, but this doesn’t seem to work.
You can just use the output of
is.nato replace directly with subsetting:Or with a reproducible example:
However, be careful using this method on a data frame containing factors that also have missing values:
It “works”:
…but you likely will want to specifically alter only the numeric columns in this case, rather than the whole data frame. See, eg, the answer below using
dplyr::mutate_if.