Re: [R] Count matches of a sequence in a vector?

2010-04-21 Thread Jeff Brown
One option might be to turn the sequence into a character string, and then use something like grep(). Kind of a kludge, but possibly easy. -- View this message in context: http://n4.nabble.com/Count-matches-of-a-sequence-in-a-vector-tp2019018p2019161.html Sent from the R help mailing list

Re: [R] Count matches of a sequence in a vector?

2010-04-21 Thread Phil Spector
If I understand what you're looking for, I think this may work: seq=c(2,3,4) v=c(4,2,5,8,9,2,3,5,6,1,7,2,3,4,5) lseq = length(seq) lv = length(v) matches = sapply(1:(lv-lseq+1),function(i)all(v[i:(i+lseq-1)] == seq)) sum(matches) [1] 1 - Phil Spector

Re: [R] Count matches of a sequence in a vector?

2010-04-21 Thread Jeff Brown
Phil's algorithm is a good one, unless you're worried about optimizing for speed. It makes N * M comparisons, where N is the length of the first vector and M is the length of the second. Explicitly iterating through the longer vector, you could reduce the number of comparisons to M. As is

[R] Count matches of a sequence in a vector?

2010-04-21 Thread mieke
Hey there, I need to count the matches of a sequence seq=c(2,3,4) in a long vector v=c(4,2,5,8,9,2,3,5,6,1,7,2,3,4,5,). With sum(v %in% seq) I only get the sum of sum(v %in% 2), sum(v %in% 3) and sum(v %in% 4), but that's not what I need :( Who can help me? Thanks a lot! -- View this

Re: [R] Count matches of a sequence in a vector?

2010-04-21 Thread Jeff Brown
This sort of calculation can't be vectorized; you'll have to iterate through the sequence, e.g. with a for loop. I don't know if a routine has already been written. -- View this message in context: http://n4.nabble.com/Count-matches-of-a-sequence-in-a-vector-tp2019018p2019108.html Sent from

Re: [R] Count matches of a sequence in a vector?

2010-04-21 Thread Gabor Grothendieck
rollapply in the zoo package could be used: library(zoo) ix - rollapply(zoo(v), 3, function(x) all(x == 2:4)) which returns a logical vector or if you want the indexes use this: which(ix) On Wed, Apr 21, 2010 at 10:16 AM, mieke hcs_ff...@gmx.de wrote: Hey there, I need to count

Re: [R] Count matches of a sequence in a vector?

2010-04-21 Thread David Winsemius
On Apr 21, 2010, at 11:07 AM, Jeff Brown wrote: At April 21, 2010 10:16:10 AM EDT mieke posted to Nabble: Hey there, I need to count the matches of a sequence seq=c(2,3,4) in a long vector v=c(4,2,5,8,9,2,3,5,6,1,7,2,3,4,5,). With sum(v %in% seq) I only get the sum of sum(v %in% 2),

Re: [R] Count matches of a sequence in a vector?

2010-04-21 Thread William Dunlap
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Jeff Brown Sent: Wednesday, April 21, 2010 8:08 AM To: r-help@r-project.org Subject: Re: [R] Count matches of a sequence in a vector? This sort of calculation can't be

Re: [R] Count matches of a sequence in a vector?

2010-04-21 Thread David Winsemius
On Apr 21, 2010, at 5:19 PM, William Dunlap wrote: -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Jeff Brown Sent: Wednesday, April 21, 2010 8:08 AM To: r-help@r-project.org Subject: Re: [R] Count matches of a sequence in a

Re: [R] Count matches of a sequence in a vector?

2010-04-21 Thread Christos Argyropoulos
It may be possible to give a solution without a single for loop. set.seed(1) v-sample(1:10,size=1e6,replace=TRUE) p-2:4 countinstance-function(v,p) { res-outer(v,p,FUN===); apply(res,2,sum) } system.time(replicate(50,countinstance(v,p)))/50 user system elapsed 0.2146 0.0248

Re: [R] Count matches of a sequence in a vector?

2010-04-21 Thread Christos Argyropoulos
Actually the memory foot print can be reduced by re-writting the function so that it makes use of the kronecker, rather than the outer function. Note that you do not get 2D map as before ... countinstance2-function(v,p) { + sapply(p,function(x,y) sum(kronecker(x,y,==)),v) + }