[R] substitute NA values

Gabor Grothendieck ggrothendieck at gmail.com
Fri Mar 30 16:46:03 CEST 2007


Not as part of na.roughfix.

You could convert your character strings to factors
and back again:

library(randomForest)
DF <- data.frame(a = c(T, F, T, NA, T),
  b = c(1:3, NA, 5),
  c = c("b", "b", NA, "d", "e"),
  d = factor(c("a", "a", NA, "d", "e")),
  stringsAsFactors = FALSE)
DF$a <- factor(DF$a)
DF$c <- factor(DF$c)
DF <- na.roughfix(DF)
DF$c <- as.character(DF$c)
DF




On 3/30/07, Sergio Della Franca <sergio.della.franca at gmail.com> wrote:
> This is that i obtained.
>
> There isn't a method to replace the NA values only for character variable?
>
>
>
>
>
>
> 2007/3/30, Gabor Grothendieck <ggrothendieck at gmail.com>:
> > I assume you are referring to na.roughfix in randomForest.  I don't think
> it
> > works for logical vectors or for factors outside of data frames:
> >
> > > library(randomForest)
> > > DF <- data.frame(a = c(T, F, T, NA, T), b = c(1:3, NA, 5))
> > > na.roughfix(DF)
> > Error in na.roughfix.data.frame(DF) : na.roughfix only works for
> > numeric or factor
> > > DF$a <- factor(DF$a)
> > > na.roughfix(DF$a)
> > Error in na.roughfix.default(DF$a) : roughfix can only deal with numeric
> data.
> > > na.roughfix(DF)
> >      a   b
> > 1  TRUE 1.0
> > 2 FALSE 2.0
> > 3  TRUE 3.0
> > 4  TRUE 2.5
> > 5  TRUE 5.0
> >
> >
> > On 3/30/07, Sergio Della Franca <sergio.della.franca at gmail.com> 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)?
> > >
> > > Thank you in advance.
> > >
> > >
> > > Sergio Della Franca
> > >
> > >        [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> >
>
>



More information about the R-help mailing list