[R] transitions in R
Philippe Grosjean
phgrosje at ulb.ac.be
Tue Jul 10 00:48:08 CEST 2001
>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?
>thanks!
>greg
transitions <- function(vect, t1, t2) {
# vect is the vector to test
# t1 is the vector of all initial states of the transitions
# t2 is the vector of all final states of the transitions
n <- length(vect)
nt <- length(t1) # rem: t2 must be same length, not tested here
v <- rep(vect, nt)
dim(v) <- c(n, nt)
v <- t(v)
tests <- (v[, 1:(n-1)]==t1 & v[, 2:n]==t2)
res <- apply(tests, 1, "sum", na.rm=T)
res
}
> p <- c(1,2,3,2,1,1,2,1,3)
> transitions(p, c(1,1,1,2,2,2), c(1,2,3,1,2,3))
[1] 1 2 1 2 0 1
...........]<(({?<...............<?}))><...............................
) ) ) ) ) __ __
( ( ( ( ( |__) | _
) ) ) ) ) | hilippe |__)rosjean
( ( ( ( ( Marine Biol. Lab., ULB, Belgium
) ) ) ) ) __
( ( ( ( ( |\ /| |__)
) ) ) ) ) | \/ |ariculture & |__)iostatistics
( ( ( ( (
) ) ) ) ) e-mail: phgrosje at ulb.ac.be or phgrosjean at sciviews.org
( ( ( ( ( SciViews project coordinator (http://www.sciviews.org)
) ) ) ) ) tel: 00-32-2-650.29.70 (lab), 00-32-2-673.31.33 (home)
( ( ( ( (
) ) ) ) ) "I'm 100% confident that p is between 0 and 1"
( ( ( ( ( L. Gonick & W. Smith (1993)
) ) ) ) )
.......................................................................
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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