How can I read data in scientific notation (D+) format into R?
e.g.
-0.416932D+01, -0.412300D+02
How can I read data in scientific notation (D+) format into R?
e.g.
-0.416932D+01, -0.412300D+02
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))
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.