0

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,

massisenergy
  • 1,764
  • 3
  • 14
  • 25
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 27 '20 at 17:01
  • Hi @MrFlick thanks for your comment. I'm not sure how to add reproducible example for a raster? Do you mean show summary like resolution, extent etc? – polarsandwich Mar 27 '20 at 17:07
  • 1
    Is there a raster you can share in your question? Perhaps a link to a public-data raster? Small and easy-to-get would be much preferred. Can you randomly generate one and not rely on external links? – r2evans Mar 27 '20 at 17:10
  • You could convert to a data frame with `as.data.frame(kiwi)`, manipulate the values in there using your favourite data.table, data.frame or tibble tools, then pack the results back into a raster with the same shape. But better (if the rasters are all on the same grid) to think of them as vectors and work with them that way. – Spacedman Mar 28 '20 at 17:32

1 Answers1

1

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)
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63