I'm trying to use R for the first time to make a histogram. I have a file containing one column of 100,000 floating-point numbers ranging in size from 8.85543e-07 to 1.15469e-03. R apparently doesn't recognize them as floating-point numbers because of the 'e' notation. How can I get R to read them. Thanks!
-
Can you please ensure that you provide a reproducible example. See [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for further details. I cannot reproduce your issue. – mnel Jul 18 '13 at 05:57
-
What do you mean by R "doesn't recognize them"? It should just read in those values as numeric values, e.g.: `is.numeric(8.85543e-07)` returns `TRUE`. How are you reading in the data? – thelatemail Jul 18 '13 at 05:58
-
I'm using data <- read.table to read in the file. I don't see any non-numeric values in the file. – user2594096 Jul 18 '13 at 06:17
4 Answers
R can read such numbers just fine; there must be another value in there that's causing the problem.
If you read in your data using read.table
/read.csv
/read.delim
, you can always convert your data to numeric if it didn't import correctly.
x <- as.numeric(as.character(df$x))
where df
is the name of your data frame, and x
is the column that you want.

- 56,353
- 13
- 134
- 187
-
Thanks - I already tried that data <- as.numeric(data) Error: (list) object cannot be coerced to type 'double' – user2594096 Jul 18 '13 at 06:17
-
1Use `as.numeric(data$x)`. You want to convert a _column within the data frame_ not the data frame itself. – Hong Ooi Jul 18 '13 at 06:20
-
1I have a similar problem and then R transforms the values with scientific notation to NAs. – Anastasia Pupynina Dec 19 '17 at 18:36
Read the file like this:
file_as_data_frame <- read.table("file.txt", colClasses="numeric")

- 31
- 2
I have found one problem with reading scientific notation in R is sometimes not the numbers themselves, but the white space between the numbers.
You can test this by cutting and pasting some of the scientific notation from your file to a text editor and surrounding the numbers with a C() function with the numbers separated by commas. If R reads the scientific notation correctly from a C() function, then you have a white space problem in your file; if not you may have a platform specific issue with the scientific notation implementation (rare and unlikely, but possible).
If you have a "whitespace issue", check your "delim = " parameter to Read.Table().
Note that:
delim = " " # one space
delim = "" # no space
delim = "/t" # escape sequence for tab
all treat white space differently. It may take trial and error to see which one works with your file.

- 31
- 1
You can set the colClasses parameter of read.tables to "character". This will cause the data to be read in character format, which will preserve the scientific notation
If you want to perform calculations on the column which has the scientific notation format, then you can use as.numeric to convert the column from character format to numeric format

- 3,690
- 1
- 15
- 24