[R] transitions in R

Thomas Lumley tlumley at u.washington.edu
Mon Jul 9 22:07:44 CEST 2001


On Mon, 9 Jul 2001, Greg Trafton wrote:

> Hi, All.  I'd have a set of data in an array:
>
> process <- c( 5 , 7 , 4 , 1 , 4 , 1 , 4 , 1 , 4 , 4 , 1 , 5 , 4 , ...)
>
> and I'd like to know the number of transitions in this data.  I
> calculate transitions as the number of times a number follows another
> number.  thus, something like this would be a 1 deep transition:
>
> 1 --> 1 : 10% (and actual number of 1 --> 1 occurrences)
> 1 --> 2 : 2%
> 1 --> 3 : 23%
> ...
> 2 --> 1 : 2%
> 2 --> 2 : 8%
> (etc.)
>
> of course, you can have 2 or 3 or n deep transitions, but I'm really
> only interested in 1 and 2 (and maybe 3) deep transitions.
>
> what is the best way of calculating this info in R?
>

One way is to just tabulate the process against an offset of itself

> process <- c( 5 , 7 , 4 , 1 , 4 , 1 , 4 , 1 , 4 , 4 , 1 , 5 , 4 )
> n<-length(process)
> table(process[-1],process[-n])

    1 4 5 7
  1 0 4 0 0
  4 3 1 1 1
  5 1 0 0 0
  7 0 0 1 0

Or if you now want to divide each column by its sum and get %ages

> tt<-table(process[-1],process[-n])
> sweep(tt,2,apply(tt,2,sum),"/")*100

     1  4  5   7
  1  0 80  0   0
  4 75 20 50 100
  5 25  0  0   0
  7  0  0 50   0


	-thomas

Thomas Lumley			Asst. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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