[R] excluding factor levels with read.table() and colClasses=

Gabor Grothendieck ggrothendieck at gmail.com
Fri Mar 17 04:21:21 CET 2006


Can you provide a reproducible example along the lines of the
following which, as seen below, does work (on R 2.2.1 Windows XP):

> x <- head(letters)
> x[2:3] <- c("NA", ".")
> x
[1] "a"  "NA" "."  "d"  "e"  "f"
> DF <- read.table(textConnection(x), na.strings = c("NA", "."))
> DF
    V1
1    a
2 <NA>
3 <NA>
4    d
5    e
6    f
> levels(DF[[1]])
[1] "a" "d" "e" "f"


On 3/16/06, Peter Tait <petertait at sympatico.ca> wrote:
> Hi,
> I did try the code with the na.strings option but it did not work. The
> factor bmicat still contains "NA" as one of its levels. Can read.table()
> exclude "NA" values from the variables it reads from test.txt? If  not
> what is the best way to remove these unwanted levels from a factor when
> programming a function?
> Thanks
> Peter
>
> >inrange<-read.table("C://test.txt", header=T, sep="|",
> na.strings=c("NA","."), colClasses=c(bmicat="factor"))
> >summary(inrange)
> bmicat
>  <23   : 294
> >28   :1482
> 23-28 :1043
> NA    :  13
> > levels(bmicat)
> [1] "<23 "   ">28 "   "23-28 " "NA "
> > contrasts(bmicat)
>       >28  23-28  NA
> <23       0      0   0
> >28       1      0   0
> 23-28     0      1   0
> NA        0      0   1
> >
>
> ______________________________________________
> 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
>




More information about the R-help mailing list