[R] Decoding raw vectors every other byte

Henrik Bengtsson hb at stat.berkeley.edu
Wed Jun 16 13:37:49 CEST 2010


x <- 1:10;
dim(x) <- c(length(x)/2, 2);
print(x);
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10
str(x[,1]);
str(x[,2]);

If length(x) is odd, some care is needed, e.g. x <- matrix(x, ncol=2,
byrow=FALSE);

/H

On Wed, Jun 16, 2010 at 1:15 PM, Alex van der Spek <doorz at xs4all.nl> wrote:
> I have to decode raw vectors read from a connection like this:
>
> +++++++++++++++++++
> fl<-file.choose()
> fb<-file.info(fl)$size
>
> fc<-file(fl)
> open(fc,open='rb')
> seek(fc,where=offset)
> dat<-readBin(fc,'raw',n=(fb-offset),size=1,signed=FALSE)
> close(fc)
> ++++++++++++++++++
>
> The decoding performs a combination of a simple bitwise operator and a
> simple arithmatic operator on every other byte in the raw vector dat.
>
> I can code this using a for loop (incrementing the loop counter by 2 every
> pass) but would rather use vectorized operations for reasons of speed. I
> can create a function with a for loop and vectorize that function but is
> there perhaps an easier way to write this? It is enough if this works on
> windows only. No need to worry about endianness.
>
> Thanks in advance.
> Alex van der Spek
>
> ______________________________________________
> 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