Skip to content

Drought severity index: Excel to R

3 messages · Muhammad Rahiz, David Winsemius, jim holtman

#
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>
#
On Jan 16, 2010, at 2:57 PM, Muhammad Rahiz wrote:

            
Not sure if this helps (because you only referenced a form of the data  
that would require significant manipulation to import correctly and  
I'n not up for that effort), but shouldn't you use the vectorized form  
of the logical conjunction operator, "&" ?
I think the outer c( ) is superfluous in this and in the prior ifelse  
clause. i:ii is a vector. Putting c() around it only confuses the  
reader.
Seems as though you should stick with numeric results. You cannot mix  
numeric and character values in a vector.
Why not use dput to offer the data to the readership?