Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I replace values in a Transition layer? (gdistance)

Tags:

r

I'm wondering if anyone could help me in replacing values in a transition layer.

If I try:

transitionlayer[transitionlayer >= 0.14] <- 1

I get the following error:

Error in hd >= 0.14 : 
  comparison (5) is possible only for atomic and list types

What I'm trying to do is replace all values above 0.14 in a slope transition layer.

I've found these examples in the gdistance vignette, however they use another transitionlayer, which I don't want to do, as I won't have the locations of cells that have a slope above 0.14

  #Extracting and replacing
  tr1[cbind(1:9,1:9)] <- tr2[cbind(1:9,1:9)]
  tr1[1:9,1:9] <- tr2[1:9,1:9]
  tr1[1:5,1:5]

Any ideas?

Thanks!

like image 203
Joseph Lewis Avatar asked Dec 17 '25 13:12

Joseph Lewis


1 Answers

First, some preparations to set up a reproducible example.

#Create a new raster and set all its values to unity.
r <- raster(nrows=18, ncols=36) 
r <- setValues(r, runif(ncell(r)))

#Create a Transition object from the raster
tr <- transition(r, transitionFunction=mean, directions=4)

Now, three options to change values in the transition matrix.

Option 1 - use S4 slots

tr@transitionMatrix@x[tr@transitionMatrix@x > 0.5] <- 0.7

Option 2 - take sparse matrix out and put back in

trM <- transitionMatrix(tr)
trM[trM > 0.5] <- 1
transitionMatrix(tr) <- trM

Option 3 - most elegant, probably

transitionMatrix(tr)[transitionMatrix(tr) < .2] <- 1
like image 146
JacobVanEtten Avatar answered Dec 19 '25 07:12

JacobVanEtten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!