I've been playing around with the airquality dataset in R and figuring out how to remove lines with missing values. I used the following command:
complete.cases(airquality) AQ1<-airquality[complete.cases(airquality),]
How do I go about replacing the NA's in airquality with 0's and then creating a new dataframe, AQ2?
P.S. Does my command above create a new dataframe called AQ1?
Thanks
To replace NA with 0 in an R data frame, use is.na() function and then select all those values with NA and assign them to 0.
How do I replace NA values on a numeric column with 0 (zero) in an R DataFrame (data. frame)? You can replace NA values with zero(0) on numeric columns of R data frame by using is.na() , replace() , imputeTS::replace() , dplyr::coalesce() , dplyr::mutate_at() , dplyr::mutate_if() , and tidyr::replace_na() functions.
To remove all rows having NA, we can use na. omit function. For Example, if we have a data frame called df that contains some NA values then we can remove all rows that contains at least one NA by using the command na. omit(df).
dataset <- matrix(sample(c(NA, 1:5), 25, replace = TRUE), 5); data <- as.data.frame(dataset)
[,1] [,2] [,3] [,4] [,5] [1,] 2 3 5 5 4 [2,] 2 4 3 2 4 [3,] 2 NA NA NA 2 [4,] 2 3 NA 5 5 [5,] 2 3 2 2 3
data[is.na(data)] <- 0
What Tyler Rinker says is correct:
AQ2 <- airquality AQ2[is.na(AQ2)] <- 0
will do just this.
What you are originally doing is that you are taking from airquality
all those rows (cases) that are complete. So, all the cases that do not have any NA's in them, and keep only those.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With