KVemuri
KVemuri

Reputation: 196

Error in number of decimal places with read.xlsx

I am trying to read in a dataset of coordinates in the British National grid system, using the read.xlsx command. This is the data:

NORTHING  EASTING TOC ELEVATION   WELL ID
1194228.31  2254272.83  117.30  AA-1
1194227.81  2254193.90  114.91  AA-2
1194228.41  2254116.26  114.76  AA-3
1194229.37  2254039.57  112.81  AA-4
1194227.09  2253960.17  112.10  AA-5

and this is my code:

coordinates <- read.xlsx2("Coordinates.xlsx",sheetName = "Sheet1",
                      startRow = 1,endRow = 111, colIndex = c(1:4),
                colClasses = c("character","character","numeric","character"))

The problem is, my output looks like this:

NORTHING EASTING TOC.ELEVATION WELL.ID 1 1194228 2254273 117.30 AA-1 2 1194228 2254194 114.91 AA-2 3 1194228 2254116 114.76 AA-3 4 1194229 2254040 112.81 AA-4 5 1194227 2253960 112.10 AA-5 6 1194227 2253880 110.98 AA-6

The command is rounding up the horizontal and vertical coordinates, and while this is not a big issue, I'd like to be as exact as possible. Is there a workaround to this? I could not find anything in the options to the colClasses option either.

Upvotes: 2

Views: 2586

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226182

This is an issue of how R is printing out the data (it is generally convenient not to give the full representation of floating-point data); you didn't actually lose any precision.

Illustrating with read.table rather than read.xlsx (we're going to end up in the same place). (If I read the data with colClasses specifying "character", I do get all of the digits displayed, but I also end up with a rather useless data frame if I want to do anything sensible with the northings and eastings variables ...)

dat <- read.table(header=TRUE,
text="
NORTHING  EASTING TOC.ELEVATION   WELL.ID
1194228.31  2254272.83  117.30  AA-1
1194227.81  2254193.90  114.91  AA-2
1194228.41  2254116.26  114.76  AA-3
1194229.37  2254039.57  112.81  AA-4
1194227.09  2253960.17  112.10  AA-5")

This is how R prints the data frame:

# NORTHING EASTING TOC.ELEVATION WELL.ID
# 1  1194228 2254273        117.30    AA-1
# 2  1194228 2254194        114.91    AA-2
# 3  1194228 2254116        114.76    AA-3
# 4  1194229 2254040        112.81    AA-4
# 5  1194227 2253960        112.10    AA-5

But it's still possible to see that all of the precision is still there ...

print(dat$NORTHING,digits=12)
## [1] 1194228.31 1194227.81 1194228.41 1194229.37 1194227.09

You could also print(dat,digits=12) or set options(digits=12) globally ...

Upvotes: 4

Related Questions