0

How can I read data in scientific notation (D+) format into R?

e.g.

-0.416932D+01, -0.412300D+02

bytecode77
  • 14,163
  • 30
  • 110
  • 141
  • 2
    Don't know if there is a direct way to handle this notation, but you can read those values as a `character` object (say `x` is the name) and then convert it to `numeric` through `as.numeric(gsub("D","e",x))`. – nicola Mar 20 '15 at 12:04
  • @nicola, post as answer? – Ben Bolker Mar 20 '15 at 12:22
  • `scan(textConnection(chartr("D", "e", "-0.416932D+01, -0.412300D+02")), sep = ",")` – G. Grothendieck Mar 20 '15 at 13:41
  • `read.fortran` looks like it should be able to deal with these numbers, but there seem to be some bugs and limitations in it. I've submitted a bug report here: https://bugs.r-project.org/bugzilla/show_bug.cgi?id=16275 – James Mar 20 '15 at 14:45

2 Answers2

2

Solution using stringr package:

library(stringr)  
x <- c("-0.416932D+03")
as.numeric(str_replace(x, "D", "e"))
[1] -416.932

If you prefer not to use external packages, you can use the gsub function from the base package:

as.numeric(gsub("D","e",x))
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Miha Trošt
  • 2,002
  • 22
  • 25
  • if you're not already using `stringr` in your code probably simpler to use `gsub()` (from base R) as suggested by @nicola in comments? – Ben Bolker Mar 20 '15 at 12:27
  • I prefer functions from Hadley W. packages in my code. – Miha Trošt Mar 20 '15 at 12:50
  • @MihaTrošt That's fine. Ben is just saying that some people prefer not to add an external dependency for something like this so as mentioned previously `gsub` is a suitable function in this case. – Dason Mar 20 '15 at 12:54
  • Should I include the `gsub` solution as well? I wonder why people do not post solutions in answers but in comments. – Miha Trošt Mar 20 '15 at 13:05
0

If you work on a Unix/Linux system, it is easiest to pre-parse your data with the following sed expression:

sed '/^#/b;s/\([-+]\?[0-9]\?[.][0-9]\+\)[DEde]\([-+]\?[0-9]\+\)/\1E\2/g' file

This wil only convert numeric strings with D or E notation into a simple E notation. The simple conversion of any D to E might break some strings.

kvantour
  • 25,269
  • 4
  • 47
  • 72