[R] Transforming sequence data into numeric values.
Greg Snow
Greg.Snow at imail.org
Fri May 7 18:00:02 CEST 2010
Here is one possibility:
tmp <- sample(c('R','L'), 100, replace=TRUE)
tmp2 <- rle(tmp)
tmp3 <- sapply( 1:length(tmp2$values),
function(i) paste( rep(tmp2$values[i], tmp2$lengths[i]), collapse='') )
table(tmp3)
the rle function computes the run lengths, the sapply line converts those results into things like "LLL", then table just counts them up.
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Simon Boardman
> Sent: Friday, May 07, 2010 4:30 AM
> To: r-help at r-project.org
> Subject: [R] Transforming sequence data into numeric values.
>
> Hi I am analysing from a behavioural experiment comparing left and
> right
> turns of snails.
>
> Basically I end up with a list of twenty values like this:
>
> R
> R
> R
> L
> R
> L
> L
> L
> L
> R
> R
> R
> R
> L
> L
> L
> R
> L
> R
> L
>
> I want to analyse runs of turns but would like to automate how this is
> processed and end up with a table that displays the frequency of the
> sequences (L, LL, LLL, R, RR, RRR etc).
>
> Is it possible to construct something like this using R and how would I
> go
> about doing it?
>
> Thanks
>
> Simon Boardman
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list