I'm trying to create a SpatialPointsDataFrame, however I get the error

Error: unexpected string constant in: "NET.TO.EXTRAC_1 = SpatialPointsDataFrame(coords = net[,3:2], data = net, proj4string = CRS(""

My code line is:

NET.TO.EXTRAC_1 = SpatialPointsDataFrame(coords = net[,3:2], data = net, proj4string = CRS("+proj=utm +zone=18 +datum=WGS84 +units=m +no_defs"))

Can someone tell me how this would be syntactically correct?

1 Answer 1


Looking closely at your error message, it shows two empty double quotes at the end of the last line (and also are there more lines missing?)...

proj4string = CRS(""

this isn't what you've got in your code line:

proj4string = CRS("+proj=utm +zone=18 +datum=WGS84 +units=m +no_defs"))

That code line works fine when I cut and paste it. So either this isn't the code line that generated the error or there's something lost in translation.

Error: unexpected string constant is a syntax error related to the basic form of an R expression - its not related to the spatial functions at all. I can generate one like this:

> x = """abc"
Error: unexpected string constant in "x = """abc""

R sees the first two " marks as making an empty string, and then it gets surprised by the "abc" that then appears. Since the projection string is the only string visible in your code line, the problem must be similar, and located there. For example this will recreate your error message:

CRS("""+proj=utm +zone=18 +datum=WGS84 +units=m +no_defs"))

Notice the three quote marks at the start.

Check carefully the matching of quote marks in your code and if it still really looks like CRS("+proj....") then type the line in again because the remaining possibility is that there are some invisible, non-printing in your code. Delete the line and type it again.

Here's a fully reproducible example that starts with a small data set of 10 points just for testing. Run this from a fresh R session and see what happens.


net = data.frame(runif(10), runif(10), runif(10))

NET.TO.EXTRAC_1 = SpatialPointsDataFrame(coords = net[,3:2], data = net, proj4string = CRS("+proj=utm +zone=18 +datum=WGS84 +units=m +no_defs"))


##                  coordinates runif.10.  runif.10..1 runif.10..2
## 1      (0.987805, 0.1249515) 0.8486718 0.1249515016  0.98780497
## 2     (0.9780648, 0.5670319) 0.7146658 0.5670319169  0.97806482
## 3     (0.9244074, 0.1650072) 0.1201483 0.1650071603  0.92440741
## 4    (0.1341612, 0.09074656) 0.9757170 0.0907465599  0.13416121
  • Hi! Thanks for your answer. I see that in the error there are two quote marks proj4string = CRS("", however I did not write the code line with two quote marks, that's why I'm confused too. I also tried retyping the code line, but still isn't working. Jul 17, 2023 at 6:14
  • Does the error message really stop at that point or are there more lines? If there's more can you please edit your question and show all the error? There's clearly something not right with the input here...
    – Spacedman
    Jul 17, 2023 at 9:35

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.