Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a raw vector as a single value in a data frame

Tags:

r

binary-data

I have some data that I need to put into a data frame in binary format (as a single entry represented by a raw vector). Here is an example:

#Record identifier
idx <- "identifier"

#The numeric data for a single record
aa <- c(5.99, 999, 5.99, 82.93)

#Convert data to double precision binary format
bin.aa <- writeBin(aa, raw(), endian = "little")

print(bin.aa)
[1] f6 28 5c 8f c2 f5 17 40 00 00 00 00 00 38 8f 40 f6 28 5c 8f c2 f5 17 40 ec 51 b8 1e 85 bb 54 40

So far so good. I have my desired raw vector. Now I want to make a data frame with two columns and a single row containing (in column 1) the "identifier" and (in column 2) the raw vector bin.aa. Unfortunately my attempt doesn't work:

> df <- data.frame(idx, bin.aa)

> head(df)
         idx bin.aa
1 identifier     f6
2 identifier     28
3 identifier     5c
4 identifier     8f
5 identifier     c2
6 identifier     f5

This is certainly not what I want to get - I now have 32 rows, each containing a single byte from the raw vector. I know it is possible to do what I'm attempting, as I can do the reverse of what I'm trying here: i.e. I can READ a raw vector into a single data frame entry using the readBin() command. Clearly I'm missing something here.

like image 251
plf27516 Avatar asked Oct 29 '25 07:10

plf27516


1 Answers

You can try:

df <- data.frame(idx, I(list(bin.aa)))

The I operator is needed here, since list(bin.aa) is a list of just one element and it gets unlisted by default in the data.frame call.

like image 112
nicola Avatar answered Oct 31 '25 12:10

nicola



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!