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

Peter Tait petertait at sympatico.ca
Fri Mar 17 04:52:14 CET 2006


Hi Gabor,

in your example X is not a factor, I don't know if this matters.
 > x <- head(letters)
 > is.factor(x)
[1] FALSE

Here is an example of my problem:
The file C://test.txt contains
id|bmicat|cat
1 |NA |.
2 |<23 |a
3 |>28 |b
4 |NA |c

 > 
test<-read.table("C://test.txt",header=T,sep="|",na.strings=c("NA","."),colClasses=c(id="factor", 
bmicat="factor", cat="factor"))
 > summary(test)
 id     bmicat    cat  1 :1   <23 :1   a   :1 2 :1   >28 :1   b   :1 3 
:1   NA  :2   c   :1 4 :1                 NA's:1 > levels(test$bmicat)
[1] "<23 " ">28 " "NA "
 > levels(test$cat)
[1] "a" "b" "c"
 >

I tried the to read this file with out the cat variable and read.table() 
recognized the "NA" properly. Adding the cat variable and its other code 
for the missing (".") seems to confuse read.table().

Thanks for your help.
Peter




More information about the R-help mailing list