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....
Asked
Active
Viewed 79 times
-2
-
1Please read it before asking a question. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – MKR Apr 03 '18 at 13:57
-
Are you trying to impute the NA values? – Highland Apr 03 '18 at 13:57
-
Please post a reproducible example. – Cristian E. Nuno Apr 03 '18 at 13:59
2 Answers
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
-
Thanks but I have a question. can I set these with another values not with 0? – ph9675 Apr 03 '18 at 14:10
-
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