2

I've noticed when I write an sf data.frame from R and open in QGIS, it seems to convert all of the numeric attribute values from integer to float (even though they should be integer). I'm not sure if this is a QGIS or R issue, but has anyone else had this problem?

I'd rather figure out if I'm doing something incorrect instead of simply converting all values to integer before exporting from R.

Below is a reproducible example in R. When the .shp is opened in QGIS, the integer values will be float.

#load built-in nc shapfile
nc <- st_read(system.file("shape/nc.shp", package="sf"))
plot(nc)

# Compute centroids of counties
centroids.nc <- st_centroid(nc)
plot(centroids.nc)

str(centroids.nc)

#export
st_write(centroids.nc, "test_out.shp")
#check in QGIS - sure enough, the integers have converted to float in QGIS

test.in<-st_read("test_out.shp")
#If I load back into R, they look fine; is it a QGIS thing? ```

1 Answer 1

1

When I run your code the only integer variable in the nc.shp on R side is CRESS_ID; and is intepreted as integer in QGIS as well.

All the other variables are "numeric" in R, which translates to Real in Qgis, in both cases being floating point (in line with expectations, with added consideration that the ESRI Shapefile follows Ashton-Tate dBase IV logic - which is positively ancient).

I am using 3.22.10-Białowieża

enter image description here

If you have numeric data on R side you wish to interpret as integer in QGIS it may help to run as.integer() on them before exporting the shapefile.

1
  • Thx Jindra; I'm using 3.20. I suppose that makes sense, I've just never noticed it before. I think you hit on the key point, which is 'numeric in R translates to Real in QGIS.' I had hoped I didn't have to use as.integer() in R before the export.
    – Tim Assal
    Commented Feb 3, 2023 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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