在R降水数据中创建循环定义的间隔开始和结束

时间:2015-06-23 11:33:59

标签: r loops time-series intervals percentage

我想分析来自南美洲的降水数据。我的目标是通过使用每年的累积降雨百分比来确定雨季的开始和偏移。

我的数据格式为

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)

我在一年内通过以下方式执行此操作:

cumsum(Precip/(sum(Precip)/100)

我想每年开始写{l} y-07-01并以(y+1)-06-30

结尾

我试过了:

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)))

我不知道如何设置间隔的开始和结束以及定义。 另外我有很多NAs。这可能是个问题吗?

1 个答案:

答案 0 :(得分:0)

当您每单位时间的值数量相同时,您尝试使用的R时间序列方法ts可能是合适的,就像有几个月和几年的情况一样每年12个月。但是,由于一年中的天数因闰年而变化,因此对于您的每日数据,您可以使用zoo包,这可能代表不规则的时间序列。使用zoo,唯一的问题是你在中间而不是在结束时开始你的一年。以下是使用它的方法之一。此代码还通过删除数据来处理数据中的NA。

library(zoo)
df <- read.table(header=TRUE, 
text="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")
precip_data <- zoo(df[,-1, drop=FALSE], order.by=as.Date(df[,1]))

#  create data for example problem
#  remove following statements to process input data
  set.seed(123)
  precip_data <- zoo(5*rexp(365*76, 100), seq(start(precip_data), by="day", length.out=365*76))
  precip_data[as.integer(365*76*runif(10000))] <- NA
#
#  calculate start and end dates of years used to accumulate data
#
  yr_start <- as.numeric( format( start(precip_data),"%Y"))
  if( start(precip_data) < as.Date(paste(yr_start,"-07-01",sep=""))) yr_start <- yr_start-1
  yr_start <- as.Date(paste(yr_start, "-07-01",sep=""))
  yr_end <- as.numeric( format( end(precip_data),"%Y"))
  if( end(precip_data) > as.Date(paste(yr_end,"-07-01",sep=""))) yr_end <- yr_end + 1
  yr_end <- as.Date(paste(yr_end, "-07-01",sep=""))
  yr_seq <- seq(yr_start, yr_end, by="year")
#
#  replace NA's with zeroes
  precip_data_zeroes <- precip_data
  precip_data_zeroes[is.na(precip_data)] <- 0
#  accumulate precipitation for each year and then divide by end of year value to compute percentages  
  cum_precip_list <- tapply(precip_data_zeroes, cut(index(precip_data_zeroes), yr_seq), cumsum)
#  list of precipitation percents by year
  cum_precip_pct <- sapply(cum_precip_list, function(x) 100*x/as.numeric(tail(x,1)))
#  zoo daily time series of precipation percents for all years
  cum_precip_zoo <- do.call(rbind, cum_precip_pct)
#  add a column with cummulative percents to original data 
  precip_data <- merge(precipitation=precip_data, cum_pct=cum_precip_zoo)
#  save precipitation data and cummulative percents as a csv file
  write.zoo(precip_data, file="precip_data.csv", index.name="Date", sep=",")

#  set the number of plots per page
  plots_per_pg <- 3
  par(mfcol=c(plots_per_pg,1))
#  set the number of years per plot
  yrs_per_plot <- 10 
  num_yr <- length(cum_precip_pct)
  num_plts <- ceiling(num_yr/yrs_per_plot)
  for(i_plt in 1:num_plts) {
    i_yr <- (i_plt-1)*yrs_per_plot+1
    i_yr_end <- min((i_yr+yrs_per_plot-1), num_yr)
    plot(cum_precip_pct[[i_yr]], xlim= c(yr_seq[i_yr], yr_seq[i_yr_end+1]),
         xlab="Year", ylab="Cummulative Precipitation (%)", cex.lab=1.4, cex.axis=1.3, xaxt="n")
    axis(side=1, at=yr_seq[i_yr:(i_yr_end+1)], labels=yr_seq[i_yr:(i_yr_end+1)], cex.axis=1.3)
    lapply((i_yr+1):i_yr_end, function(x) lines(cum_precip_pct[[x]]))
   }

cum_precip_pct包含累计百分比作为每年zoo时间序列的R列表。 precip_data的第一列包含原始降水数据,在第二列中包含整个期间的累计百分比。

<强>更新

上面的代码已被修改为用零替换NA&,将累积百分比数据作为列添加到原始降水数据,并将其作为csv文件保存到磁盘。

我不清楚你的策划意图。将七十多年的每日数据放在每年与每隔一年非常相似的情节中并没有显示出太多细节。作为一种更灵活的替代方案,代码现在可以在绘图上显示多年的数据,每页有几个绘图。每页的绘图数量设置为3,每个绘图的年数设置为10,但您可以根据需要更改这些。这些图的一个例子如下所示。

enter image description here