2

I am extremely new to QGIS and to GIS software generally, so apologies in advance if the answer here is obvious.

I have used QGIS to create intersection percentages and maps based on two different types of geographies for a city: Census blocks and city council districts. I have followed the excellent Q&A here (Percentage of polygon in one shapefile within polygon of another) to execute the necessary intersection and calculate the proportional allocations for each resulting geography. I have also used the unique identifiers from each table to create a variable that assigns a unique identifier to each Block-District pairing.

I need to use the resulting data in a statistical analysis using R. When I export the attribute table for the Intersection layer to a CSV file, my unique identifier variable--a 17-digit TEXT variable created in QGIS--imports as numeric and changes some of its values on R import, with no discernible explanation why. (Meaning it comes in with different values for the last two digits.) When opening the same file in Excel, all of the values for the identifier variable round to the hundredths place, replacing the last two digits with 00.

Can anyone provide guidance for how to export from QGIS to open in R and Excel while maintaining the correct values? Does anyone have some insight as to why this is happening?

I'm not sure how to provide "code" demonstrating what's going on, but I've listed out the difference in observations between programs below.

First Entry

QGIS

Block ID: 480219503001036
District ID: 02
Correct Unique ID: 48021950300103602 (populates correctly in QGIS)

RStudio (incorrect)

Block ID: 480219503001036
District ID: 02
Unique ID: 48021950300103600 (INCORRECT)

Excel (incorrect)

Block ID: 480219503001036
District ID: 02
Unique ID: 48021950300103600 (INCORRECT)

Second Entry

QGIS

Block ID: 482090109011000
District ID: 08
Correct Unique ID: 48209010901100008 (populates correctly in QGIS)

RStudio

Block ID: 482090109011000
District ID: 08
Unique ID: 48209010901100008 (populates correctly in RStudio, but classified as numeric)

Excel

Block ID: 482090109011000
District ID: 08
Unique ID: 48209010901100000 (INCORRECT)

Fourth Entry

QGIS

Block ID: 482090109011009
District ID: 08
Unique ID: 48209010901100908

RStudio

Block ID: 482090109011009
District ID: 08
Unique ID: 48209010901100912 (INCORRECT)

Excel

Block ID: 482090109011009
District ID: 08
Unique ID: 48209010901100900 (INCORRECT)
5

You need to tell R that columns in your CSV are significant as characters, and are not a sequence of digits making up a number.

Do this with the colClasses argument.

For example, a CSV like this:

ID, value
48021950300103602,48021950300103602

where ID is to be interpreted as character, and value as a number, needs to be read in with:

> d = read.csv("bad.csv",colClasses=c("character","numeric"))

to see how much precision is lost in the number, we first have to ask R to show more digits than normal:

> options(digits=20)

Then you can see that the character variable, ID, has got all its characters:

> d$ID
[1] "48021950300103602"

but the value, read in as numeric, has lost some:

> d$value
[1] 48021950300103600

because R (like most programming languages' default arithmetic processing) cannot store integers to an indefinite precision.

Without colClasses R makes a guess on what sort of column each one is, and in your case gets it wrong. If your identifiers started with a letter, eg BLOCK48021950300103602, then R would guess correctly and you wouldn't have to mess with colClasses. If you can do this, I would strongly recommend doing something like that (I don't know if you've made up these IDs or if they are some sort of central authority standard IDs...).

| improve this answer | |
  • Thank you so much. I figured it was a simple solution I'd overlooked, and the specification using colClasses seems to have worked. For anyone that needs a reference of how to specify the classes of particular columns on import (for a large csv where specifying column-by-column may be undesirable), I found this related R stack helpful (particularly the second highest response): stackoverflow.com/questions/2805357/… – Abe Barranca Apr 5 at 2:34
  • As to the practicalities of adding a string to the beginning of the IDs, I suppose I could do that. The leading 15 digits are decennial Census FIPS geographic identifiers. – Abe Barranca Apr 5 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.