[R] Drought severity index: Excel to R
Muhammad Rahiz
muhammad.rahiz at ouce.ox.ac.uk
Sat Jan 16 20:57:31 CET 2010
Dear all,
I'm trying to make an R code for the drought severity index (DSI)
developed by Philips and McGregor (1998). You can refer to the
description of the algorithm on page 19 from
http://dissertations.port.ac.uk/229/01/MaloneS.pdf
The code is given in Excel as the following and can be found on page 60
from the same link.
C7 =
IF(C6<0,IF(@SUM(A6:A1)<0,C6+A6,"0"),
IF(B7>=0,"0",IF(A6>=0,"0",A6)))
Column A contains the raw anomaly data. Column B contains the 6month
rolling sum and Column C contains the results of the conditional
statement which in turn is recycled and input into the conditional
statement.
I translated the Excel formula into R, but without any success.
x <- as.matrix(read.table("sample.txt")) # where sample.txt contains
values of anomalies. See page 60
ct <- 6 # sets a 6-month averaging sequence
n <- ct -1
d <- matrix( ,32,1) # dummy file
# User defined function
add <- function(x) Reduce("+", x)
for (i in 1:32){
ii <- i + n
a <- i +1
d[[a]] <-
ifelse(d[ii] < 0 && add(x[c(i:ii)]) < 0, d[ii] + x[ii], ( # condition 1
ifelse(add(x[c((i+1):(ii+1))]) >= 0, 0, ( # condition 2
ifelse(x[ii] >=0,"0", x[ii]))))) # condition 3
}
The way I see it, this is the main problem.
How do I make the data in Excel's Column C in R? Or in other words, how
do I update the result of the conditional statements into the dummy
file, d, which I created (which apparently didn't work).
I hope my explanation makes sense.
thanks.
Muhammad
--
Muhammad Rahiz | Doctoral Student in Regional Climate Modeling
Climate Research Laboratory, School of Geography & the Environment
Oxford University Centre for the Environment, University of Oxford
South Parks Road, Oxford, OX1 3QY, United Kingdom
Tel: +44 (0)1865-285194 Mobile: +44 (0)7854-625974
Email: muhammad.rahiz at ouce.ox.ac.uk<mailto:muhammad.rahiz at ouce.ox.ac.uk>
More information about the R-help
mailing list