Search code examples
rdataframeraster

Using dplyr-like commands to organise raster data in R


I've got three global soil texture rasters (sand, clay and silt). I want to merge these rasters into one raster with two categories (coarse and fine) based on relative percentages of sand, clay and silt. I've done this before when working with in this way:

kiwi <- kiwi %>% mutate(group = case_when(
clay_value_avg < 20  ~ "coarse",
silt_value_avg > 80 ~ "coarse",
clay_value_avg > 20 ~ "fine",
silt_value_avg < 80 ~ "fine"
))

Can I do something like this with ? Thanks,


Solution

  • You cannot use this type of syntax, but there are other ways

    This is how you create a simple and self-contained reproducible example

    library(raster)
    clay <- silt <- raster(ncol=10, nrow=10)
    values(clay) <- 1:100
    values(silt) <- 99:0
    

    This is an approach

    fine <- silt < 80 & clay > 20
    coarse <- !fine
    

    And another

    f <- function(s, c) {
        s < 80 & c > 20
    }
    fine <- overlay(silt, clay, fun=f)