[R] How to speed up int2bin conversion?

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Thu Dec 1 17:25:34 CET 2011


You might be interested in package "bit".
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

"Jonas Jägermeyr" <jonasjae at pik-potsdam.de> wrote:

>Dear R-help members,
>
>I'm processing a large amount of MODIS data where quality assessment 
>information is stored as an integer value for each pixel. I have to 
>converted this number to an 8 digit binary flag to get access to the 
>stored quality code (e.g. in2bin(165,8) = 1 0 1 0 0 1 0 1).
>
>Unfortunately, I did not manage to find a package providing a fast 
>function to do so. I need to run this on millions of pixels and thus 
>wrote the following function.
>
>int2bin <- function(x,ndigits){
>     base <- array(NA, dim=c(length(x), ndigits))
>     for(q in 1:ndigits){
>           base[, ndigits-q+1] <- (x %% 2)
>           x <- (x %/% 2)
>       }
>     bin<- apply(base,1,paste,collapse="")
>     return(bin)
>}
>
>Since it is still slow, I have to find a way to express this more 
>elegantly. I'd really appreciate any help.
>
>Thanking you, with best regards
>
>Jonas J�germeyr
>
>Potsdam Institute for Climate Impact Research
>Research Domain II
>
>	[[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