Try this:

DS <- structure(list(anomaly = c(-27.3, 14.4, 29, -38.1, -3.4, 55.1,
-1, 21.9, -10.9, -7.8, -48.2, -44.9, -43.8, -10.3, 44.2, -0.5,
96.7, -32, -0.2, -38.6, 73.6, -17.5, -57.8, 10.7, -33.4, 46.1,
26.7, -37.3, 1.2, 36.3, 39.6, 31), prev6mon = c(NA, NA, NA, NA,
NA, 29.7, 56, 63.5, 23.6, 53.9, 9.1, -91, -133.8, -166, -110.8,
-103.5, 41.4, 54.3, 97.9, 69.6, 99.1, 82.1, -72.4, -29.7, -62.9,
21.7, -25.2, -45.1, 14, 39.6, 112.7, 97.6), DSI = c(NA, NA, NA,
NA, NA, NA, 0, 0, 0, 0, 0, -48.17, -93.08, -136.93, -147.21,
-102.98, -103.47, 0, 0, 0, 0, 0, -17.47, -75.29, -64.57, -98.01,
0, 0, 0, 0, 0, 0), DSIadj = c(NA, NA, NA, NA, NA, NA, 0, 0, 0,
0, 0, 0.06, 0.12, 0.17, 0.18, 0.13, 0.13, 0, 0, 0, 0, 0, 0.02,
0.09, 0.08, 0.12, 0, 0, 0, 0, 0, 0), DSIpct = c(NA, NA, NA, NA,
NA, NA, 0, 0, 0, 0, 0, 6.02, 11.64, 17.12, 18.4, 12.87, 12.94,
0, 0, 0, 0, 0, 2.18, 9.41, 8.07, 12.25, 0, 0, 0, 0, 0, 0), Month =
structure(c(5L,
4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L,
9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L), .Label = c("April ", "Aug ", "Dec ", "Feb ", "Jan", "July ",
"June ", "March ", "May ", "Nov ", "October ", "Sept "), class = "factor"),
   Yr = c(1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L,
   1980L, 1980L, 1980L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L,
   1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1982L, 1982L,
   1982L, 1982L, 1982L, 1982L, 1982L, 1982L)), .Names = c("anomaly",
"prev6mon", "DSI", "DSIadj", "DSIpct", "Month", "Yr"), class = "data.frame",
row.names = c(NA,
-32L))
DS  # original data
# now only use the first column of data and construct columns B & C
# fill in the 'prev6mon' column using filter and zero the DSI column
DS$prev6mon <- filter(DS$anomaly, rep(1,6), side=1)
DS$DSI <- 0
# iterate since you have to refer to a previous value
for (i in 7:nrow(DS)){
    DS$DSI[i] <- with(DS, {
        if (DSI[i - 1] < 0) {
            if (prev6mon[i - 1] < 0) DSI[i - 1] + anomaly[i - 1]
            else 0
        }
        else {
            if (prev6mon[i] >= 0) 0
            else {
                if (anomaly[i - 1] >= 0) 0
                else anomaly[i - 1]
            }
        }
    })
}



On Sat, Jan 16, 2010 at 2:57 PM, Muhammad Rahiz <
muhammad.rahiz@ouce.ox.ac.uk> 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
> 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@ouce.ox.ac.uk<mailto:muhammad.rahiz@ouce.ox.ac.uk>
>
> ______________________________________________
> 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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

	[[alternative HTML version deleted]]

