[R] reading in hexadecimal data - not working with data ending in E

William Dunlap wdunlap at tibco.com
Wed Oct 8 21:58:15 CEST 2014


I did not see a reproducible example, but you should use
colClasses="character" to
read in the data as character, then use something like as.hexmode() to
convert it to integers.  E.g.,

  > z <- read.table(text="ColA ColB\n1e 33\n2e  44\n10  5e\n",
header=TRUE, colClasses="character")
  > str(z)
  'data.frame':   3 obs. of  2 variables:
   $ ColA: chr  "1e" "2e" "10"
   $ ColB: chr  "33" "44" "5e"
  > z[] <- lapply(z, function(zi)as.integer(as.hexmode(zi)))
  > str(z)
  'data.frame':   3 obs. of  2 variables:
   $ ColA: int  30 46 16
   $ ColB: int  51 68 94

If you leave out the colClasses="character" then read.table will say
that all those
strings look like decimal numerals ("1e" being read as "1e+0", giving 1*10^0).


Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Wed, Oct 8, 2014 at 4:02 AM,  <mark.hogue at srs.gov> wrote:
> I am trying to read in data from an instrument that is recorded in
> hexadecimal format. Using either:
>
>     y.hex <- read.table(file="hex.data", as.is=TRUE)
>
> or
>
>     y.hex <- read.table(file="hex.data", text=TRUE)
>
> gets all the data in just fine except points like `055E` or `020E`. In
> these cases, the E is stripped so I get just 055 or 020.
>
> The question is how should this data be imported to avoid the E-ending
> problem?
>
> (By the way, my follow-up is to convert this data using, `y <-
> strtoi(y.hex, 16L)`)
>
> Thanks for any suggestions,
>
> Mark Hogue
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list