I want to analyse precipitation data from South America. My objective is to determinate onset and offset of the rainy season by using cumulative percentage rainfall of each year.
My data has the format
Date Precip
1939-11-01 0
1939-11-02 0
1939-11-03 0
1939-11-04 4.9
1939-11-05 0
1939-11-06 0.7
1939-11-07 3.5
lapply1<-read.table("lapply.txt",header=T)
lapply1$Date<-as.Date(lapply1$Date)
For a single year I do this by:
cumsum(Precip/(sum(Precip)/100)
I want to write a lopp starting at every year y-07-01
and ending at (y+1)-06-30
I tried this:
lapply(lapply, function(cumsum(lapply1$Precip/(sum(lapply1$Precip)/100)),
ts(lapply1, freq=4, start=c(y-01-01), end=c(y+1-06-30), names(lapply1)))
I dont know how to set the start and end of the interval and to define. Additionaly I have many NAs. Could this be a problem?
The R time series method
ts
, which you're trying to use, may be appropriate when there are an equal number of values per unit of time as in the case of months and years where there are 12 months in every year. However, since the number of days in a year changes due to leap year, for your daily data you might use thezoo
package, which can represent irregular time series. Usingzoo
, the only issue is that you start your year in the middle rather than at the end. The following is one of way of working with that. This code also deals with NA's in the data by removing them.cum_precip_pct
contains the cumulative percents as an R list ofzoo
time series for each year. The first column ofprecip_data
contains the original precipitation data and, in the second column, the cumulative percents for the entire period.UPDATE
The code above has been modified to replace NA's with zeroes, to add the cummulative percent data as a column to the original precipitation data, and to save this as a csv file to disk.
I'm not clear on your intention for the plotting. Putting seventy something years of daily data in one plot where every year is very similar to every other year doesn't show much detail. As a more flexible alternative, the code can now display multiple years of data on a plot with several plots per page. The number of plots per page is set at 3 and the number of years per plot is set at 10 but you can change these as need be. An example of these plots is shown below.