在 R 降水数据中创建循环定义间隔开始和结束
create loop in R precipitation data defined interval start and end
我想分析南美洲的降水数据。我的 objective 是通过每年的累积降雨百分比来确定雨季的开始和偏移。
我的数据格式为
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)
我想写一个从每年 y-07-01
开始到 (y+1)-06-30
结束的 lopp
我试过这个:
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)))
我不知道如何设置间隔的开始和结束以及如何定义。
另外我有很多 NA。这会是个问题吗?
您尝试使用的 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 文件保存到磁盘。
我不清楚你的阴谋意图。将 70 多年的日常数据放在一个每年与其他年份非常相似的图中并不能显示太多细节。作为一种更灵活的替代方案,该代码现在可以在一个图表上显示多年的数据,每页有多个图表。每页的图数设置为 3,每图的年数设置为 10,但您可以根据需要更改这些。这些图的示例如下所示。
我想分析南美洲的降水数据。我的 objective 是通过每年的累积降雨百分比来确定雨季的开始和偏移。
我的数据格式为
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)
我想写一个从每年 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)))
我不知道如何设置间隔的开始和结束以及如何定义。 另外我有很多 NA。这会是个问题吗?
您尝试使用的 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 文件保存到磁盘。
我不清楚你的阴谋意图。将 70 多年的日常数据放在一个每年与其他年份非常相似的图中并不能显示太多细节。作为一种更灵活的替代方案,该代码现在可以在一个图表上显示多年的数据,每页有多个图表。每页的图数设置为 3,每图的年数设置为 10,但您可以根据需要更改这些。这些图的示例如下所示。