[R] transitions in R
Jonathan Baron
baron at cattell.psych.upenn.edu
Mon Jul 9 23:01:39 CEST 2001
>From owner-r-help at stat.math.ethz.ch Mon Jul 9 16:43:54 2001
>Received: from stat.math.ethz.ch (majordom at hypatia.ethz.ch [129.132.58.23])
> by cattell.psych.upenn.edu (8.9.1/8.9.1/SAS.05) with ESMTP id QAA10528
> for <baron at cattell.psych.upenn.edu>; Mon, 9 Jul 2001 16:43:54 -0400 (EDT)
>Received: by stat.math.ethz.ch (8.9.1/8.9.1) id VAA22595
> for r-help-gang-use; Mon, 9 Jul 2001 21:58:06 +0200 (MET DST)
>Received: (from daemon at localhost)
> by stat.math.ethz.ch (8.9.1/8.9.1) id VAA22574
> for <r-help at hypatia.math.ethz.ch>; Mon, 9 Jul 2001 21:57:58 +0200 (MET DST)
>Received: from viz.itd.nrl.navy.mil(132.250.80.179)
> via SMTP by hypatia, id smtpdAAAa005WR; Mon Jul 9 21:57:48 2001
>Received: (from trafton at localhost)
> by viz.itd.nrl.navy.mil (8.11.2/8.11.2) id f69Jvfq17493;
> Mon, 9 Jul 2001 15:57:41 -0400
>From: Greg Trafton <trafton at itd.nrl.navy.mil>
>MIME-Version: 1.0
>Content-Type: text/plain; charset=us-ascii
>Content-Transfer-Encoding: 7bit
>Message-ID: <15178.3253.53696.310509 at viz.itd.nrl.navy.mil>
>Date: Mon, 9 Jul 2001 15:57:41 -0400
>To: r-help at stat.math.ethz.ch
>Subject: [R] transitions in R
>X-Mailer: VM 6.89 under 21.1 (patch 14) "Cuyahoga Valley" XEmacs Lucid
>Sender: owner-r-help at stat.math.ethz.ch
>Precedence: SfS-bulk
>Status: R
>Content-Length: 985
>
>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?
For the one-deep try
plength <- length(process)
table(process[1:(plength-1)],process[2:plength])
To get proportions,
table(process[1:(plength-1)],process[2:plength])/(plength-1)
Multiply by 100 to get percent, and use round() if needed.
I think you can extend this to more depth and get bigger tables.
Jon Baron
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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