[R] relationship between two factors

David Winsemius dwinsemius at comcast.net
Fri Dec 7 17:26:02 CET 2007


"Henrique Dallazuanna" <wwwhsd at gmail.com> wrote in 
news:da79af330712061144m738a9bd6ob8ecb2e89e8ff2c3 at mail.gmail.com:

> Try this:
> 
> df <- data.frame(DMS=factor(rep(LETTERS[1:4], 10)),
> ITS=factor(rep(LETTERS[1:4], 10)))
> table(df)
> plot(table(df))
> 

That gave me simply 10's on the diagonals of the table and the plot was 
not very satisfying. I suggest this alternate example:

tbl<-r2dtable(1,c=4*c(15,12,8,5),r=4*c(20,10,5,5))
tbl
#[[1]]
#     [,1] [,2] [,3] [,4]
#[1,]   27   23   21    9
#[2,]   18   11    5    6
#[3,]    8    6    2    4
#[4,]    7    8    4    1

# > class(tbl)
# [1] "list"
ITS=factor(rep(LETTERS[1:4]))
DMS=factor(rep(LETTERS[1:4]))
df<-data.frame(counts=unlist(tbl),expand.grid(DMS,ITS))
names(df)<-c("counts","DMS",   "ITS")
df
(df.tbl<-xtabs(counts~DMS+ITS,df))
#       ITS
#DMS  A  B  C  D
#      A 27 23 21  9
#      B 18 11  5  6
#      C  8  6  2  4
#      D  7  8  4  1
plot(df.tbl)

-- 
David Winsemius



More information about the R-help mailing list