Reputation: 311
Hi I am a beginner with R (beginner programmer in general) and the help documents are absolutely killing me.
Suppose I have a matrix [a,b,c,d] I complete 2 regression of some kind a~b+c+d. My goal is to do a predict() for the variable "a" in test data set but c is full of NAs. How do I replace the NAs in c using the model I have created?
If it helps this is the kind of loop I would do in Octave,
for i:length(c)
if c(i)=NA
c(i)=some_function(b,d);<---- I tried to bold this but it came out wrong
end
Thanks
Upvotes: 0
Views: 845
Reputation: 121127
It's even easier than Seb suggests.
c[is.na(c)] <- mean(c, na.rm = TRUE)
Here, the mean function returns a single number (namely the mean of all the values in c
that weren't NA
). The assignment operator <-
then assigns this number to every element in c
where is.na
returns TRUE
.
As an alternative, try passing the argument na.action = na.omit
to the predict function.
The direct translation of your Octave script is something like
for(i in seq_along(c))
{
if(is.na(c[i]))
{
c(i) <- some_function(b[i], d[i])
}
}
Note however that in R, just as in Octave, loops are usually inferior to operating directly on vectors.
Upvotes: 1
Reputation: 5507
do you mean something like
c <- ifelse(is.na(c), mean(c, na.rm=TRUE), c)
you may want to check the help files ?ifelse
and ?is.na
.
Upvotes: 0