-2

I have two sets, training set and test set that there are some values that they are NA, I need a code or a guidance in R language that set values for NA. Thank you....

ph9675
  • 75
  • 1
  • 8

2 Answers2

0
df <- data.frame(a = sample(c(NA, 1:5),10, replace = T))

  a
  5
  5
  3
  2
 NA
  5
  1
 NA
  5
  4

Now replace the NA with any value. Please note that in this case I replace all NA's with 0

df[is.na(df)] <- 0

Result

 a
 5
 5
 3
 2
 0
 5
 1
 0
 5
 4
Rana Usman
  • 1,031
  • 7
  • 21
0

You asked in subsequent comments if you can replace NA's with values other than zeros. You can. One method is mean imputation (The following code is reproduced from r bloggers:

df = data.frame(x = 1:20, y = c(1:10,rep(NA,10)))
df$y[is.na(df$y)] = mean(df$y, na.rm=TRUE)

It is important that you understand whether mean imputation is appropriate for your data (or any other imputation for that manner) short blog post on problems with mean imputation

Just use caution.

Lee
  • 224
  • 1
  • 2
  • 9