[R] substitute NA values
Jim Lemon
jim at bitwrit.com.au
Sat Mar 31 12:51:26 CEST 2007
Sergio Della Franca wrote:
> Dear R-Helpers,
>
>
> I have the following data set(y):
>
> Test_Result #_Test
> t 10
> f 14
> f 25
> f NA
> f 40
> t 45
> t 44
> <NA> 47
> t NA
>
>
> I want to replace the NA values with the following method:
> - for the numeric variable, replace NA with median
> - for character variable , replace NA with the most frequent level
>
> If i use x<-na.roughfix(y) the NA values are correctly replaced.
> But if i x<-na.roughfix(y$Test_Result) i obtain the following error:
>
> roughfix can only deal with numeric data.
>
> How can i solve this proble that i met every time i want to replace only the
> NA values of a column (type character)?
>
Hi Sergio,
In the prettyR package is the Mode function. This returns the mode of a
vector as a character string. So I think this would do what you want:
library(prettyR)
testvec<-c(sample(LETTERS[1:4],20,TRUE),NA,NA)
testvec[is.na(testvec)]<-Mode(testvec)
You could do the same trick with the median function for the numeric values.
Jim
More information about the R-help
mailing list