2

I have a list of raw-vectors named "output". Something like that:

[1] 58 0a 00 00 00 03 00 04 00 03 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00 00 fe
[1] 58 0a 00 00 00 03 00 04 00 03 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 01 03 19 00 00 04 02 00 00 00 01 00 04 00 09 00 00 00 04 6d 65 74 61 00 00 02 13 00 00 00 03 00 00 00 10 00 00 00 01 00
[1] ...

They have different lenghts and are from the type "raw".

I need a dataframe with one vector in each cell:

ID vectors
1 58 0a 00 00 00 03 00 04 00 03 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00 00 fe
2 58 0a 00 00 00 03 00 04 00 03 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 01 03 19 00 00 04 02 00 00 00 01 00 04 00 09 00 00 00 04 6d 65 74 61 00 00 02 13 00 00 00 03 00 00 00 10 00 00 00 01 00

I have tried this:

as.data.frame(output) 
#Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 27, 3132, 4141, 4267, 3701, 3943, 5200

df <- data.frame(matrix(unlist(output), nrow=length(output)))
#Warning message:
In matrix(unlist(output), nrow = length(output)) :
  data length [32954] is not a sub-multiple or multiple of the number of rows [14]

Is there a way to solve my problem?

Flow91
  • 63
  • 6

2 Answers2

3

You have to use I when creating the data.frame.

output <- list(raw(2), raw(3))
DF <- data.frame(ID=1:2, vectors = I(output))

str(DF)
#'data.frame':   2 obs. of  2 variables:
# $ ID     : int  1 2
# $ vectors:List of 2
#  ..$ : raw  00 00
#  ..$ : raw  00 00 00
#  ..- attr(*, "class")= chr "AsIs"

DF
#DF
#  ID    vectors
#1  1     00, 00
#2  2 00, 00, 00
GKi
  • 37,245
  • 2
  • 26
  • 48
  • Oh thank you! Never saw that I() function before... But its works great! – Flow91 Jul 15 '21 at 07:40
  • When Im creating the dataframe on this way, I found a problem in the cells of vectors. It should contain something like: `58 0a 00 00 00 03 00 04 00 03 00 03 05 00 00 00 00 05 55 54 46 2d 38 00` but I have now: `as.raw(c(0x58, 0x0a, 0x00, 0x00, 0x00, 0x03, 0x00))`. – Flow91 Jul 15 '21 at 13:24
  • Which problem have you found? – GKi Jul 15 '21 at 13:25
  • The structure of the dataframe `str(DF)` is the same but if im open the dataframe with `DF` the cells in the vectors-column contain as.raw(c(0x58...)) instead of 58 0a 00... – Flow91 Jul 15 '21 at 13:39
  • You should get with `DF$vectors` the same what you will get with `output`. – GKi Jul 15 '21 at 14:03
  • Yeah with `DF$vectors` and with `output` its the same. But with `DF` im getting a table with `as.raw(c(0x58, 0x0a, 0x00...))` in the vector cells. Thats a problem because I want to create a database with this dataframe and im getting `Error: UNIQUE constraint failed` all the time. – Flow91 Jul 16 '21 at 08:29
  • When im write `class(output[[1]])` then the output is `[1] "raw"`. When im write `class(DF[1,2])` then the output is `"AsIs"`. Maybe there is another way to create the dataframe with type `raw`? – Flow91 Jul 16 '21 at 08:52
  • Maybe `attr(DF$vectors, "class") <- NULL` could help. – GKi Jul 19 '21 at 06:18
1

This can be also done with tibble

library(tibble)
output <- list(raw(2), raw(3))
tibble(ID = 1:2, vectors = output)
# A tibble: 2 x 2
     ID vectors  
  <int> <list>   
1     1 <raw [2]>
2     2 <raw [3]>
akrun
  • 874,273
  • 37
  • 540
  • 662