[R] Extracting a range of elements from a vector

Otis Laws msc22ojl at cs.bham.ac.uk
Sat Aug 25 13:43:14 CEST 2007

Dear R users

I am R newbie creating a function that implements the poker test to test 
pseudo random bit generators.
Iam reading the bits from a text file (1 bit per line),  which causes 
each bit to be stored in an element of a numeric vector.

What Iam trying to do is to extract a "block" of bits of arbitray size 
from the original vector into a smaller numeric vector and then count 
this binary number
 (and keep repeating this until the end of the vector, so that I get a 
vector containing the number of times each binary number has occured) e.g.

original vector:
"0", "1","1","0","0","1","0","1","1"

using a block size of 3 bits the first smaller vector becomes:
"0", "1", "1"

At the momemt I do this by iterating through the original vector and set 
the ith element of the smaller vector.
I have looked at using the subset() function but it seems to operate on 
a vector's content rather than index.

This causes the following two main questions:
1. Is there a way to specify a range of vector elements? 
2. Is this the most efficient method, since this could be extremly time 
consuming when used to test millions of bits?

Thanks very much in advance

Otis Laws

More information about the R-help mailing list