I seem to have a simple problem in R which I've found no simple solution for: I would like to convert an array of integers to raw. Conversion between other simple types have a similar problem. I have found the following non-elegant solutions:
- use as.raw and create a new array with the old dimensions:
a <- array(255:261,c(2,3))
b <- array(as.raw(a),dim(a))
- use as.raw and reset the dimension attribute:
a <- array(255:261,c(2,3))
b <- as.raw(a)
dim(b) <- dim(a)
- change the storage mode
a <- array(255:261,c(2,3))
b <- a
storage.mode(b) <- "raw"
All solutions are really complex for something that should be simple. In solution 1 and 2 as.raw (as other as.* functions) deletes the dim attribute, hence it must be reset. Solution 2 and 3 do not directly support the functional programming style, i.e., they do not return a copy of a with the new type, and, thus, cannot be used easily with piping. I can, of course, make a new function to support functional programming, but my guess is that I'm overlooking something basic. Please enlighten me. Thanks.