Alex Rivera | Logout

How to display numeric columns in an R dataframe without scientific notation ('e+07')

Asked 2011-07-15T12:01:21.310
14

I have an R dataframe with one column containing a stringt of numbers but I would like to treat them as a factor (mainly to stop R shortening the numbers using e+04 etc...). One way I have found to fix this problem is to edit the csv file the data is taken from, and add a dummy entry that has a word in the desired column and then reimporting it. How do I get this effect using R functions without messing around with the csv?

To clarify, my dataframe looks like this:

pNum,Condition,numberEntered
1,2,5.0970304e+07

I want to change the data type of numberEntered from numeric to factor and get rid of the pesky e+07.

Edit
Report

1 Answer

32

As Joshua said, it is a printing issue not a storage issue. You can change the way all numbers are printed (=by adjusting getOption("scipen").

x <- c(1, 2, 509703045845, 0.0001)
print(x)
options(scipen = 50)
print(x)

Alternatively, you may wish to change the way just those numbers are formatted. (This converts them to character.) It is worth getting to know format and formatC. To get you started, compare

format(x)
format(x, digits = 10)
format(x, digits = 3)
format(x, digits = 3, scientific = 5)
format(x, trim = TRUE, digits = 3, scientific = 5)
formatC(x)
formatC(x, format = "fg")
formatC(x, format = "fg", flag = "+")
answered 2011-07-15T13:22:24.097

Your Answer