0

I am trying to calculate change in habitat cover from over time, starting with T0 and based on disturbance occurrence (Disturbance_freq) and recovery (Recovery_rate) in R. I need to loop this calculations for each Region based on Total_Disturbance (which is different for each region).

My Data is setup as such:

Region  Distur_rate Tot_dist  Dist_freq   Impact    Recovery_Rate   T0          T1
0       0.29        29        3.45        20        0.47            0.59        ?
1       0.17        17        5.88        20        0.47            0.59        ?
2       0.4         40        2.5         20        0.47            0.59        ?

I am trying to calculate:

T1 = (((T0 + Recovery_Rate x Dist_freq) - (20 x (T0 + Recovery_Rate x Dist_freq)/100)) 
T2 = (((T1 + Recovery_Rate x Dist_freq) - (20 x (T1 + Recovery_Rate x Dist_freq)/100))

etc...

To do so I coded the following but there must be a more efficient way to do this:

Data <-cbind(Data, (Data$T0 + Data$Recovery_rate * Data$Disturbance_freq)- Data$Impact * 
                     (Data$T0+Data$Recovery_rate * Data$Disturbance_freq)/100)

Data <-cbind(Data, (Data$T1 + Data$Recovery_rate * Data$Disturbance_freq)-Data$Impact * 
                    (Data$T1+Data$Recovery_rate * Data$Disturbance_freq)/100)
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • I think it is best to post a small example that people can work with along with workable input data and expected output. – Gopala Jan 29 '16 at 01:45
  • Please edit your post with a sample dataset (preferably the result of `dput( ... )`), and show what you want your results to look like, i.e. offer a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – alistaire Jan 29 '16 at 01:45

1 Answers1

0

Your brackets there do not seem to be correct so you will need to look at them. But this is a case for apply. Call your table there data

data$T1 <- apply(data,1,function(x) {
    (((x[7] + x[6]) * x[4]) - (x[5] * (x[7] + x[6]) * x[4])/100)
    })
data$T2 <- apply(data,1,function(x) {
    (((x[8] + x[6]) * x[4]) - (x[5] * (x[8] + x[6]) * x[4])/100)
    })
JeremyS
  • 3,497
  • 1
  • 17
  • 19