Change cell value in one raster based on another raster











up vote
2
down vote

favorite












I have two raster maps from two points in time (t1 and t2) with two land-cover categories in each (LC1, LC2). I want impose a rule that a LC2-cell in t1 cannot change to LC1-cell in t2, i.e., only LC1 can change to LC2 through time but not the other way around. I am having a hard time coming up with a rule for that in R. What I had in mind was something like this:



#create test rasters
r <- raster(nrows=25, ncols=25, vals=round(rnorm(625, 3), 0)) #land-use/cover raster
r[ r > 2 ] <- 2
r[ r < 1 ] <- 1
r2 <- r
plot(r2) #r2 is t2

r <- raster(nrows=25, ncols=25, vals=round(rnorm(625, 3), 0)) #land-use/cover raster
r[ r > 2 ] <- 2
r[ r < 1 ] <- 1
plot(r) #r is t1

r_fix <- overlay(r, r2, fun = function(x, y) {
if (x[ x==2 ] & y[ y==1 ]) { #1 is LC1, 2 is LC2
x[ x==2 ] <- 1 }
return(x)
})


But it returns an error (because of they way I am using the if statement with rasters?):



Error in (function (x, fun, filename = "", recycle = TRUE, forcefun = FALSE,  : 
cannot use this formula, probably because it is not vectorized


I wonder if there is a simple way to implement something similar to that that works with rasters? Thank you in advance.










share|improve this question




























    up vote
    2
    down vote

    favorite












    I have two raster maps from two points in time (t1 and t2) with two land-cover categories in each (LC1, LC2). I want impose a rule that a LC2-cell in t1 cannot change to LC1-cell in t2, i.e., only LC1 can change to LC2 through time but not the other way around. I am having a hard time coming up with a rule for that in R. What I had in mind was something like this:



    #create test rasters
    r <- raster(nrows=25, ncols=25, vals=round(rnorm(625, 3), 0)) #land-use/cover raster
    r[ r > 2 ] <- 2
    r[ r < 1 ] <- 1
    r2 <- r
    plot(r2) #r2 is t2

    r <- raster(nrows=25, ncols=25, vals=round(rnorm(625, 3), 0)) #land-use/cover raster
    r[ r > 2 ] <- 2
    r[ r < 1 ] <- 1
    plot(r) #r is t1

    r_fix <- overlay(r, r2, fun = function(x, y) {
    if (x[ x==2 ] & y[ y==1 ]) { #1 is LC1, 2 is LC2
    x[ x==2 ] <- 1 }
    return(x)
    })


    But it returns an error (because of they way I am using the if statement with rasters?):



    Error in (function (x, fun, filename = "", recycle = TRUE, forcefun = FALSE,  : 
    cannot use this formula, probably because it is not vectorized


    I wonder if there is a simple way to implement something similar to that that works with rasters? Thank you in advance.










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have two raster maps from two points in time (t1 and t2) with two land-cover categories in each (LC1, LC2). I want impose a rule that a LC2-cell in t1 cannot change to LC1-cell in t2, i.e., only LC1 can change to LC2 through time but not the other way around. I am having a hard time coming up with a rule for that in R. What I had in mind was something like this:



      #create test rasters
      r <- raster(nrows=25, ncols=25, vals=round(rnorm(625, 3), 0)) #land-use/cover raster
      r[ r > 2 ] <- 2
      r[ r < 1 ] <- 1
      r2 <- r
      plot(r2) #r2 is t2

      r <- raster(nrows=25, ncols=25, vals=round(rnorm(625, 3), 0)) #land-use/cover raster
      r[ r > 2 ] <- 2
      r[ r < 1 ] <- 1
      plot(r) #r is t1

      r_fix <- overlay(r, r2, fun = function(x, y) {
      if (x[ x==2 ] & y[ y==1 ]) { #1 is LC1, 2 is LC2
      x[ x==2 ] <- 1 }
      return(x)
      })


      But it returns an error (because of they way I am using the if statement with rasters?):



      Error in (function (x, fun, filename = "", recycle = TRUE, forcefun = FALSE,  : 
      cannot use this formula, probably because it is not vectorized


      I wonder if there is a simple way to implement something similar to that that works with rasters? Thank you in advance.










      share|improve this question















      I have two raster maps from two points in time (t1 and t2) with two land-cover categories in each (LC1, LC2). I want impose a rule that a LC2-cell in t1 cannot change to LC1-cell in t2, i.e., only LC1 can change to LC2 through time but not the other way around. I am having a hard time coming up with a rule for that in R. What I had in mind was something like this:



      #create test rasters
      r <- raster(nrows=25, ncols=25, vals=round(rnorm(625, 3), 0)) #land-use/cover raster
      r[ r > 2 ] <- 2
      r[ r < 1 ] <- 1
      r2 <- r
      plot(r2) #r2 is t2

      r <- raster(nrows=25, ncols=25, vals=round(rnorm(625, 3), 0)) #land-use/cover raster
      r[ r > 2 ] <- 2
      r[ r < 1 ] <- 1
      plot(r) #r is t1

      r_fix <- overlay(r, r2, fun = function(x, y) {
      if (x[ x==2 ] & y[ y==1 ]) { #1 is LC1, 2 is LC2
      x[ x==2 ] <- 1 }
      return(x)
      })


      But it returns an error (because of they way I am using the if statement with rasters?):



      Error in (function (x, fun, filename = "", recycle = TRUE, forcefun = FALSE,  : 
      cannot use this formula, probably because it is not vectorized


      I wonder if there is a simple way to implement something similar to that that works with rasters? Thank you in advance.







      r if-statement raster






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 20:50









      Julius Vainora

      27k75877




      27k75877










      asked Nov 10 at 20:34









      Thales West

      3312417




      3312417
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You were really close,



          overlay(r, r2, fun = function(x, y) {x[x == 2 & y == 1] <- 1; x})


          seems to do the job.



          In terms of your solution,



          x[x == 2] <- 1


          doesn't cause any errors, although it's not exactly what you want to use in your case either. However,



          if (x[x == 2] & y[y == 1])


          is a problem because x[x == 2] & y[y == 1] returns a matrix, while if wants just a single logical input. Subsetting, on the other hand, can handle logical matrices, which is exactly what is happening in x[x == 2 & y == 1].






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243163%2fchange-cell-value-in-one-raster-based-on-another-raster%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            You were really close,



            overlay(r, r2, fun = function(x, y) {x[x == 2 & y == 1] <- 1; x})


            seems to do the job.



            In terms of your solution,



            x[x == 2] <- 1


            doesn't cause any errors, although it's not exactly what you want to use in your case either. However,



            if (x[x == 2] & y[y == 1])


            is a problem because x[x == 2] & y[y == 1] returns a matrix, while if wants just a single logical input. Subsetting, on the other hand, can handle logical matrices, which is exactly what is happening in x[x == 2 & y == 1].






            share|improve this answer

























              up vote
              2
              down vote



              accepted










              You were really close,



              overlay(r, r2, fun = function(x, y) {x[x == 2 & y == 1] <- 1; x})


              seems to do the job.



              In terms of your solution,



              x[x == 2] <- 1


              doesn't cause any errors, although it's not exactly what you want to use in your case either. However,



              if (x[x == 2] & y[y == 1])


              is a problem because x[x == 2] & y[y == 1] returns a matrix, while if wants just a single logical input. Subsetting, on the other hand, can handle logical matrices, which is exactly what is happening in x[x == 2 & y == 1].






              share|improve this answer























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                You were really close,



                overlay(r, r2, fun = function(x, y) {x[x == 2 & y == 1] <- 1; x})


                seems to do the job.



                In terms of your solution,



                x[x == 2] <- 1


                doesn't cause any errors, although it's not exactly what you want to use in your case either. However,



                if (x[x == 2] & y[y == 1])


                is a problem because x[x == 2] & y[y == 1] returns a matrix, while if wants just a single logical input. Subsetting, on the other hand, can handle logical matrices, which is exactly what is happening in x[x == 2 & y == 1].






                share|improve this answer












                You were really close,



                overlay(r, r2, fun = function(x, y) {x[x == 2 & y == 1] <- 1; x})


                seems to do the job.



                In terms of your solution,



                x[x == 2] <- 1


                doesn't cause any errors, although it's not exactly what you want to use in your case either. However,



                if (x[x == 2] & y[y == 1])


                is a problem because x[x == 2] & y[y == 1] returns a matrix, while if wants just a single logical input. Subsetting, on the other hand, can handle logical matrices, which is exactly what is happening in x[x == 2 & y == 1].







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 20:43









                Julius Vainora

                27k75877




                27k75877






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243163%2fchange-cell-value-in-one-raster-based-on-another-raster%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues