Skip to main content
Updated answer to account for terra library
Source Link
Jeffrey Evans
  • 32k
  • 2
  • 48
  • 97

Things work a bit differently in R than ArcGIS, at least on the surface. You cannot "join" a data.frame to a raster in the way you are thinking. Rasters store a numeric array and do not contain attribute tables. The ESRI raster model is the same, even when there is an associated VAT, it is just obscured by the GUI.

You could emulate this behavior using ratify but, it really is not necessary and could cause some headaches down the road. The only good reason that I have found to ratify a raster with an attribute table is for plotting purposes else wise, you can just use a data.frame. All you need here is a data.frame with a unique identifier that matches the raster values and columns containing new values. In this case I believe that you are simply looking for reclassify.

The idea is that you have a patch ID raster and a corresponding flat file relating the patch ID to each patch-level landscape metric. Now, the intent is to create rasters representing each metric, correct?

First, lets create a patch ID raster and an associate data.frame with PID, AREA and iji columns.

library(raster)

frag <- raster(extent(5305396, 5340783, -2247132, -2227007),
               nrow=300, ncol=528)
    frag[] <- sample(1:500, ncell(frag), replace=TRUE)
    
( patch.df <- data.frame(PID=1:500, 
                         AREA=runif(500, 10, 500), 
                         iji=runif(500, 0, 1) ) )

Now, the reclassify function can take a two or three column matrix. In the case of three columns the first two represent a range and the third the new value. In the case of two columns, the first is the original value (ie., patch ID) and the second is the new value (ie., iji). This syntax simply pulls the Patch ID and iji columns (1,3) and coerces to a matrix within reclassify. You can then write the result out to a new on-disk raster.

( iji <- reclassify(frag, as.matrix(patch.df[,c(1,3)])) )

****Edit (09/10/24) For terra rasters use the levels function with a data.frame where the value column represents the values in your raster that match the attribution and the second column contains attributes. eg., assuming you have a raster with values 1-10 and attributes 10-20

levels(x) <- data.frame(value=1:10, att=10:20) 

Things work a bit differently in R than ArcGIS, at least on the surface. You cannot "join" a data.frame to a raster in the way you are thinking. Rasters store a numeric array and do not contain attribute tables. The ESRI raster model is the same, even when there is an associated VAT, it is just obscured by the GUI.

You could emulate this behavior using ratify but, it really is not necessary and could cause some headaches down the road. The only good reason that I have found to ratify a raster with an attribute table is for plotting purposes else wise, you can just use a data.frame. All you need here is a data.frame with a unique identifier that matches the raster values and columns containing new values. In this case I believe that you are simply looking for reclassify.

The idea is that you have a patch ID raster and a corresponding flat file relating the patch ID to each patch-level landscape metric. Now, the intent is to create rasters representing each metric, correct?

First, lets create a patch ID raster and an associate data.frame with PID, AREA and iji columns.

library(raster)

frag <- raster(extent(5305396, 5340783, -2247132, -2227007),
               nrow=300, ncol=528)
    frag[] <- sample(1:500, ncell(frag), replace=TRUE)
    
( patch.df <- data.frame(PID=1:500, 
                         AREA=runif(500, 10, 500), 
                         iji=runif(500, 0, 1) ) )

Now, the reclassify function can take a two or three column matrix. In the case of three columns the first two represent a range and the third the new value. In the case of two columns, the first is the original value (ie., patch ID) and the second is the new value (ie., iji). This syntax simply pulls the Patch ID and iji columns (1,3) and coerces to a matrix within reclassify. You can then write the result out to a new on-disk raster.

( iji <- reclassify(frag, as.matrix(patch.df[,c(1,3)])) )

Things work a bit differently in R than ArcGIS, at least on the surface. You cannot "join" a data.frame to a raster in the way you are thinking. Rasters store a numeric array and do not contain attribute tables. The ESRI raster model is the same, even when there is an associated VAT, it is just obscured by the GUI.

You could emulate this behavior using ratify but, it really is not necessary and could cause some headaches down the road. The only good reason that I have found to ratify a raster with an attribute table is for plotting purposes else wise, you can just use a data.frame. All you need here is a data.frame with a unique identifier that matches the raster values and columns containing new values. In this case I believe that you are simply looking for reclassify.

The idea is that you have a patch ID raster and a corresponding flat file relating the patch ID to each patch-level landscape metric. Now, the intent is to create rasters representing each metric, correct?

First, lets create a patch ID raster and an associate data.frame with PID, AREA and iji columns.

library(raster)

frag <- raster(extent(5305396, 5340783, -2247132, -2227007),
               nrow=300, ncol=528)
    frag[] <- sample(1:500, ncell(frag), replace=TRUE)
    
( patch.df <- data.frame(PID=1:500, 
                         AREA=runif(500, 10, 500), 
                         iji=runif(500, 0, 1) ) )

Now, the reclassify function can take a two or three column matrix. In the case of three columns the first two represent a range and the third the new value. In the case of two columns, the first is the original value (ie., patch ID) and the second is the new value (ie., iji). This syntax simply pulls the Patch ID and iji columns (1,3) and coerces to a matrix within reclassify. You can then write the result out to a new on-disk raster.

( iji <- reclassify(frag, as.matrix(patch.df[,c(1,3)])) )

****Edit (09/10/24) For terra rasters use the levels function with a data.frame where the value column represents the values in your raster that match the attribution and the second column contains attributes. eg., assuming you have a raster with values 1-10 and attributes 10-20

levels(x) <- data.frame(value=1:10, att=10:20) 
added 380 characters in body
Source Link
Jeffrey Evans
  • 32k
  • 2
  • 48
  • 97

Things work a bit differently in R than ArcGIS, at least on the surface. You cannot "join" a data.frame to a raster in the way you are thinking. Rasters store a numeric array and do not contain attribute tables. The ESRI raster model is the same, even when there is an associated VAT, it is just obscured by the GUI.

You could emulate this behavior using ratify the raster but, it really is not at all necessary and could cause some headaches down the road. The only good reason that I have found to ratify a raster with an attribute table is for plotting purposes else wise, you can just use a data.frame. All you need here is a data.frame with a unique identifier that matches the raster values and columns containing new values. In this case I believe that you are simply looking for reclassify. 

The idea is that you have a patch ID raster and a corresponding flat file relating the patch ID to each patch-level landscape metric. Now, the intent is to create rasters representing each metric, correct?

First, lets create a patch ID raster and an associate data.frame with PID, AREA and iji columns.

library(raster)

frag <- raster(extent(5305396, 5340783, -2247132, -2227007),
               nrow=300, ncol=528)
    frag[] <- sample(1:500, ncell(frag), replace=TRUE)
    
( patch.df <- data.frame(PID=1:500, 
                         AREA=runif(500, 10, 500), 
                         iji=runif(500, 0, 1) ) )

Now, the reclassify function can take a two or three column matrix. In the case of three columns the first two represent a range and the third the new value. In the case of two columns, the first is the original value (ie., patch ID) and the second is the new value (ie., iji). This syntax simply pulls the Patch ID and iji columns (1,3) and coerces to a matrix within reclassify. You can then write the result out to a new on-disk raster.

( iji <- reclassify(frag, as.matrix(patch.df[,c(1,3)])) )

Things work a bit differently in R than ArcGIS, at least on the surface. You could ratify the raster but, it really is not at all necessary and could cause some headaches down the road. I believe that you are simply looking for reclassify. The idea is that you have a patch ID raster and a corresponding flat file relating the patch ID to each patch-level landscape metric. Now, the intent is to create rasters representing each metric, correct?

First, lets create a patch ID raster and an associate data.frame with PID, AREA and iji columns.

library(raster)

frag <- raster(extent(5305396, 5340783, -2247132, -2227007),
               nrow=300, ncol=528)
    frag[] <- sample(1:500, ncell(frag), replace=TRUE)
    
( patch.df <- data.frame(PID=1:500, 
                         AREA=runif(500, 10, 500), 
                         iji=runif(500, 0, 1) ) )

Now, the reclassify function can take a two or three column matrix. In the case of three columns the first two represent a range and the third the new value. In the case of two columns, the first is the original value (ie., patch ID) and the second is the new value (ie., iji). This syntax simply pulls the Patch ID and iji columns (1,3) and coerces to a matrix within reclassify. You can then write the result out to a new on-disk raster.

( iji <- reclassify(frag, as.matrix(patch.df[,c(1,3)])) )

Things work a bit differently in R than ArcGIS, at least on the surface. You cannot "join" a data.frame to a raster in the way you are thinking. Rasters store a numeric array and do not contain attribute tables. The ESRI raster model is the same, even when there is an associated VAT, it is just obscured by the GUI.

You could emulate this behavior using ratify but, it really is not necessary and could cause some headaches down the road. The only good reason that I have found to ratify a raster with an attribute table is for plotting purposes else wise, you can just use a data.frame. All you need here is a data.frame with a unique identifier that matches the raster values and columns containing new values. In this case I believe that you are simply looking for reclassify. 

The idea is that you have a patch ID raster and a corresponding flat file relating the patch ID to each patch-level landscape metric. Now, the intent is to create rasters representing each metric, correct?

First, lets create a patch ID raster and an associate data.frame with PID, AREA and iji columns.

library(raster)

frag <- raster(extent(5305396, 5340783, -2247132, -2227007),
               nrow=300, ncol=528)
    frag[] <- sample(1:500, ncell(frag), replace=TRUE)
    
( patch.df <- data.frame(PID=1:500, 
                         AREA=runif(500, 10, 500), 
                         iji=runif(500, 0, 1) ) )

Now, the reclassify function can take a two or three column matrix. In the case of three columns the first two represent a range and the third the new value. In the case of two columns, the first is the original value (ie., patch ID) and the second is the new value (ie., iji). This syntax simply pulls the Patch ID and iji columns (1,3) and coerces to a matrix within reclassify. You can then write the result out to a new on-disk raster.

( iji <- reclassify(frag, as.matrix(patch.df[,c(1,3)])) )
Source Link
Jeffrey Evans
  • 32k
  • 2
  • 48
  • 97

Things work a bit differently in R than ArcGIS, at least on the surface. You could ratify the raster but, it really is not at all necessary and could cause some headaches down the road. I believe that you are simply looking for reclassify. The idea is that you have a patch ID raster and a corresponding flat file relating the patch ID to each patch-level landscape metric. Now, the intent is to create rasters representing each metric, correct?

First, lets create a patch ID raster and an associate data.frame with PID, AREA and iji columns.

library(raster)

frag <- raster(extent(5305396, 5340783, -2247132, -2227007),
               nrow=300, ncol=528)
    frag[] <- sample(1:500, ncell(frag), replace=TRUE)
    
( patch.df <- data.frame(PID=1:500, 
                         AREA=runif(500, 10, 500), 
                         iji=runif(500, 0, 1) ) )

Now, the reclassify function can take a two or three column matrix. In the case of three columns the first two represent a range and the third the new value. In the case of two columns, the first is the original value (ie., patch ID) and the second is the new value (ie., iji). This syntax simply pulls the Patch ID and iji columns (1,3) and coerces to a matrix within reclassify. You can then write the result out to a new on-disk raster.

( iji <- reclassify(frag, as.matrix(patch.df[,c(1,3)])) )