Reading binary file into R

后端 未结 1 686
孤街浪徒
孤街浪徒 2021-01-06 04:09

I\'m trying to read a binary file into R but this file has rows of data written in binary code. So it doesnt have one full set of data belonging to one column instead it is

1条回答
  •  情话喂你
    2021-01-06 05:03

    You could open the file as a raw file, then issue readBin or readChar commands to get each line. Append each value to a column as you go.

    my.file <- file('path', 'rb')
    
    id <- integer(0)
    response <- character(0)
    ...
    

    Loop around this block:

    id = c(id, readBin(my.file, integer(), size = 4, endian = 'little'))
    response = c(response, readChar(my.file, 1))
    ...
    readChar(my.file, size = 1) # For UNIX newlines.  Use size = 2 for Windows newlines.
    

    Then create your data frame.

    See here: http://www.ats.ucla.edu/stat/r/faq/read_binary.htm

    0 讨论(0)
提交回复
热议问题