[R] Constructing a transition matrix

Chris Stubben stubben at lanl.gov
Wed Dec 7 18:52:13 CET 2005

Hi Peter,

Thanks for pointing out the set functions.  I can use setdiff to find 
missing rows

setdiff(dev, rownames(A))
[1] "seed"

and intersect to find common rows

d1<- intersect(dev, rownames(A) )
[1] "veg" "rep"

I was trying to use a negative index like A[-1,] to remove the dead row, 
but d1 is a better solution.  Now I can add the missing seed row and get 
a square matrix.

rbind( seed=numeric(3), A[d1,] )[dev,dev]

Another post by Hans Gardfjell suggested reordering factor levels before 
using prop.table(table()) and this solution works great!

trans$class <- ordered(trans$class, levels=dev)
trans$fate <- ordered(trans$fate, levels=c(dev,"dead") )

A <- t(prop.table(table(trans$class, trans$fate),1))[-4,]

             seed veg rep
   seed 0.0000000   0 0.0
   veg  0.6666667   0 0.5
   rep  0.0000000   1 0.5

Thanks for the help,


Peter Dalgaard wrote:

> Are you looking for something like
> d1 <- setdiff(dev,"seed")
> A0[d1,dev] <- A[d1,dev]
> ?

More information about the R-help mailing list