[R] argument 'exclude' in xtabs

Matthieu Lesnoff matthieu.lesnoff at gmail.com
Thu Apr 23 15:53:27 CEST 2009


Dear all

I was willing to use argument 'exclude' in function xtabs to remove some
levels of factors (xtabs help page says '"exclude: a vector of values to be
excluded when forming the set of levels of the classifying factors"). 


I tried:

> mydata <- data.frame(
+     treatment = c("B", "A", "C", "C", "B", "B", "C", "A", "B", "B", NA,
"C"),
+     surv = c("YES", "NO", "YES", "YES", "NO", "NO", "NO", "YES", "YES",
"NO", "NO", NA)
+     )

> levels(mydata$treatment)
[1] "A" "B" "C"

> # try to remove level "B" in variable treatment
> xtabs(formula = ~ treatment + surv, data = mydata, exclude = "B")
         surv
treatment NO YES
        A  1   1
        B  3   2
        C  1   2



One alternative is to do:

> xtabs(formula = ~ treatment + surv, data = mydata[mydata$treatment != "B",
], drop.unused.levels = TRUE)
         surv
treatment NO YES
        A  1   1
        C  1   2



But I don't understand why I cannot remove "B" directly with argument
exclude.

Any help is welcome

Regards

Matthieu





My version on Windows XP

platform       i386-pc-mingw32             
arch           i386                        
os             mingw32                     
system         i386, mingw32               
status                                     
major          2                           
minor          9.0                         
year           2009                        
month          04                          
day            17                          
svn rev        48333                       
language       R                           
version.string R version 2.9.0 (2009-04-17)





------------------
Matthieu Lesnoff
CIRAD
BP 1813, Quartier Niaréla, Immeuble Kouma
Bamako, Mali
E-mail: matthieu.lesnoff at cirad.fr 




More information about the R-help mailing list