Home > Software design >  How to have R read CSV's actual values instead of Exponential format
How to have R read CSV's actual values instead of Exponential format

Time:10-27

I'm trying to read a bank file with one of the columns having numbers mixed with the name holder, for example the column might have:

2.10731E 15
JOE BX SHOP
2.10802E 15
etc. 

The actual value of the 2.10731E 15 type files are: 2107310000000000, however because there are some strings present R is reading the column as a character and thus displaying the values only as 2.10731E 15.

Is there a way to make R ignore the csv file's formatting when reading in using read.csv or read_csv?

CodePudding user response:

If we don't mind storing numbers as characters, then we could check if it is a number, then convert to a number and format without scientific notation:

# example data
x <- read.csv(text = "
myCol
2.10731E 15
JOE BX SHOP
2.10802E 15")

# get index of rows with numbers
ix <- !is.na(as.numeric(x$myCol))
#Warning message:
#  NAs introduced by coercion 

# format matching rows
x[ ix, "myCol"] <- format(as.numeric(x[ ix, "myCol"]), scientific = FALSE)

x
#              myCol
# 1 2107310000000000
# 2      JOE BX SHOP
# 3 2108020000000000
  •  Tags:  
  • r csv
  • Related