[R] Table help

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Sep 26 19:30:19 CEST 2001


João Gama <jgama at liacc.up.pt> writes:

> Hi,
> A quick question:
> I have to vectors, say ar1 and ar2
> 
> > ar1
>     [1] "a" "c" "c" "a"
>     attr(,"levels")
>     [1] "a" "b" "c"
> 
> > ar2
>     [1]  TRUE  TRUE FALSE  TRUE
> 
> > table(ar1, ar2)
>        ar2
> ar1 FALSE TRUE
>   a     0    2
>   c     1    1
> 
> 
> I would like to obtain:
> 
>     T    F
> a  2    0
> b  0    0
> c  1    1
> 
> The problem seems to be that level "b" in ar1 doesn't appear in ar1.
> and Function "table" doesn't have any drop.unused.levels parameter.

ar1 is not a factor there (giving it a levels attribute won't make it
a factor). Try

> ar1<-factor(c("a","c","c","a"),levels=c("a","b","c"))
> ar2<-c(T,T,F,T)
> table(ar1, ar2)
   ar2
ar1 FALSE TRUE
  a     0    2
  b     0    0
  c     1    1


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list