[Traminer-users] searching several subsequences

Alexis gabadinho Alexis.Gabadinho at unige.ch
Tue Sep 27 20:03:09 CEST 2011


Hi Marcelo,

Nice to know that TraMineR is used to analyse birds songs. Before we add 
this feature in the next TraMineR version, what I can do for you is this 
modified version of the seqpm function. You can copy-paste it into your 
console.

seqpm2 <- function(seqdata, pattern) {

     if (!inherits(seqdata,"stslist")) {
         stop("data is not a sequence object, use 'seqdef' function to 
create one")
     }

     match <- matrix(0, nrow=nrow(seqdata), ncol=1)
     rownames(match) <- rownames(seqdata)
     colnames(match) <- "matches"

     pl <- length(seqdecomp(pattern, sep=""))

     for (i in 1:(ncol(seqdata)-pl)) {
         tmp <- seqconc(seqdata[,i:(i+(pl-1))],sep="")
         pm <- grep(pattern,tmp)
         match[pm] <- match[pm]+1
     }


     return(match)
}

This function returns a vector with the number of matches in each 
sequence of your sequence object:

 > s1 <- seqdef("A-B-C-A-B-A-B-A-B-A-B-A-B-A-B-A-B-C-A-B")

 > seqpm2(s1, "ABC")
     matches
[1]       2


If you have several sequences

 > ex <- c("A-B-C-A-B-A-B-A-B-A-B-A-B-A-B-A-B-C-A-B", 
"A-B-B-A-B-A-B-A-B-A-B-A-B-A-B-A-B-C-A-B")

 > ex.seq <- seqdef(ex)

 > ex.seq
     Sequence
[1] A-B-C-A-B-A-B-A-B-A-B-A-B-A-B-A-B-C-A-B
[2] A-B-B-A-B-A-B-A-B-A-B-A-B-A-B-A-B-C-A-B

 > seqpm2(ex.seq, "ABC")
     matches
[1]       2
[2]       1

By the way, it is easier for us if you subscribe to the users list (see 
http://mephisto.unige.ch/traminer/contrib.shtml) before sending 
questions. And if you write a paper about birds songs using TraMineR, we 
would be pleased to add it to our "who uses it" page!

All the best,
Alexis




Le 26. 09. 11 02:33, Marcelo Araya a écrit :
>
> Hi all
>
> I am analyzing bird song element sequences. I would like to know how 
> can I get how many times a given subsequence is found in single string 
> sequence.
>
> For example:
>
> If I have this single sequence:
>
> *ABC*ABABABABABAB*ABC*AB
>
> If I am looking for the subsequence "ABC". Want I need to get here is 
> that the subsequence is found twice. The  function "seqpm" just tell 
> me if the subsequence is there or not.
>
> Thanks in advance
>
> Marcelo Araya-Salas
>
> Ph.D.Student
>
> Avian Communication and Evolution Lab
>
> Department of Biology
>
> New Mexico State University
>
> Lab: 575-646-4863
>
>
> _______________________________________________
> Traminer-users mailing list
> Traminer-users at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/traminer-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/traminer-users/attachments/20110927/22e7ec2e/attachment.htm>


More information about the Traminer-users mailing list