1

I am using R to analyze some raster files using the packages "raster", "rgdal" and "tigris" in my workspace. I'm trying to use the function spTransform on a dataset using the following code:

transStates <- spTransform(threeStates, CRS(fire@crs@projargs))

However when I run this code I get the following error:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘spTransform’ for signature ‘"sf", "CRS"’

A similar question was asked in another thread and they suggested using st_transform if it is a data.file class but R doesn't recognise that function. What could be the problem in this case?

1
  • If the other “thread” was on this site, can you provide a link to that Q&A, please?
    – PolyGeo
    Commented Aug 10, 2021 at 7:29

2 Answers 2

1

A similar question was asked in another thread and they suggested using st_transform if it is a data.file class but R doesn't recognise that function. What could be the problem in this case?

There are two different, incompatible spatial data types in R with two different, packages with differently-named functions to work with them.

The older types use the sp and rgdal packages, and create data called things like "SpatialPolygonsDataFrames". You use spTransform to transform those.

The newer types use the sf package, and use functions beginning st_ to work on them, such as st_transform to change coordinate systems.

You have tried to use an sp function on an sf data object, hence the error:

unable to find an inherited method for function ‘spTransform’ for signature ‘"sf", "CRS"’

Without knowing exactly how you got an sf object (threeStates) or what you intend to do with it its hard to give an exact way for you to proceed here. But to convert from sp to sf you can so st_as_sf(something), and to convert the other way from sf to sp you can do as(thing, "Spatial").

So...

install the sf package with install.packages("sf") and then after doing library(sf) then:

spTransform(as(threeStates,"Spatial"), CRS(fire@crs@projargs))`

might get you past this (although we have no idea what fire is...)

0

I found a solution and it was the same as one of the ones you suggested. I used the code "as(raster_file, 'Spatial') instead of "as_Spatial" and it worked. Must have been an issue with the file type, but the code has been copied directly from the markdown of an online tutorial so I don't know why it didn't work initially.

Thanks for the help!

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.