0

This is a sample table for the data I need to extract from a PostgreSQL database. Sample data

As you can see, the iv_file (it's a hexadecimal number) column in is in Bytea format. So when I try to extract onlu the iv_file column in R, I get the following.

`iv_file
1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 \\x
2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 \\x
3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 \\x
4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 \\x
5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 \\x
6 \\x364630353037303036463035303830303646303530443030364530353142303036453035333730303643303535463030364230353838303036393035414530303638303544313030363630354634303036353035313630313634303533373031363330353537303136323035373730313630303539373031354630354236303135453035443630313544303546363031354330353136303235423035333430323539303535343032353930353733303235373035393130323536303542313032353530354430303235343035463030323533303530453033353230353245303335303035344430333446303536423033344530353842303334443035414130333443303543383033344130354538303334413035303830343438303532373034343730353435303434363035363530343434303538343034343430354132303434333035433230343431303545313034343030353031303533463035314630 

The data is not the same as the original data. (Please note that the original data has a long number too) So How do I use bytea data format in R? or how do I convert it?

Nisal
  • 43
  • 1
  • 12

1 Answers1

0

I dont believe you can convert it. Bytea data is basically bite data. I would suggest just not pulling it into the rdata frame. Bytea is normally bitdata that is an image or something that can not be stored in a traditional format.

Another approach would be to use a JDBC, that normally takes care of the transformations. I would take a look at require(RPostgreSQL) to get a sense of how it can be done.

You might be able to convert it back once it is loaded into R. Try something like this

udecode <- function(string){
  uconv <- function(chars) intToUtf8(strtoi(chars, 16L))
  ufilter <- function(string) {
  if (substr(string, 1, 1)=="|") uconv(substr(string, 2, 5)) else string
 }
 string <- gsub("\\\\u([[:xdigit:]]{4})", ",|\\1,", string, perl=TRUE)
 strings <- unlist(strsplit(string, ","))
 string <- paste(sapply(strings, ufilter), collapse='')
 return(string)
}
patchus
  • 97
  • 4
  • Isn't it possible to import it as string? – Nisal Nov 03 '15 at 05:40
  • Yes you could import it as a string, but its a formatting issue. Your postgres is setup in unicode (which you can tell from the \\x piece). You might be able to convert it back once it is loaded into R. Try something like this – patchus Nov 03 '15 at 14:15
  • Tried it. The function wasn't really a success. Still getting the old output. – Nisal Nov 04 '15 at 01:33
  • the bytea is read into R as a hex encoded character string. You have to convert it. See answer here: https://stackoverflow.com/a/50999966/2371031 – Brian D Dec 06 '18 at 15:08