On Jan 16, 2010, at 2:57 PM, Muhammad Rahiz wrote:
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
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, "&" ?
ifelse(add(x[c((i+1):(ii+1))]) >= 0, 0, ( # condition 2
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.
ifelse(x[ii] >=0,"0", x[ii]))))) # condition 3
Seems as though you should stick with numeric results. You cannot mix
numeric and character values in a vector.
}
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).
Why not use dput to offer the data to the readership?
I hope my explanation makes sense.
thanks.
Muhammad
--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.