Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replacing NA's with 0's in R dataframe [duplicate]

Tags:

r

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

like image 950
killahtron Avatar asked Sep 01 '13 20:09

killahtron


People also ask

How do you replace all NA's with 0 in R?

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 with 0 in a column?

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.

How do I remove Na's from a DataFrame in R?

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).


2 Answers

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 
like image 81
lbenitesanchez Avatar answered Oct 08 '22 12:10

lbenitesanchez


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.

like image 36
PascalVKooten Avatar answered Oct 08 '22 13:10

PascalVKooten