如何将时间序列保存在 in r 循环中创建的数据帧中?

问题描述 投票:0回答:1

我有几个雨量计(以列形式)的每日降雨量时间序列,并且正在计算一些年度指数,例如年度降水量(PRCPTOT)。我使用循环生成每个雨量计的年度总计的系列,我需要按雨量计将结果保存在数据框中。 我怎样才能保存它?

library(precintcon)
library(reshape2)
library(plyr)

#Creating a data.frame exemple using precintcon package
df=melt(daily,id = c("year","month"))
df$value2=df$value;df$value3=df$value;df$value4=df$value
colnames(df) <- c('year','month','day','RainGaue1','RainGaue2','RainGaue3','RainGaue4') 
head(df)

#Looping
ncol(df)
nproc = 4
cont = 1
#Data.frame with results
dfr = data.frame(variavel = character(nproc), 
                  PRCTOT = numeric(nproc),
                  stringsAsFactors = F)
# i = 2
for (i in 2:5) {
  
  print(i) 
  df2<-df[,-c(2,3)]#
  nome.var=colnames(df2[i])
  variavela = nome.var[1]
  dfr$variavel[cont] = variavela

  df2 = df2[,c(1,i)]
  df2 = na.omit(df2)
  names(df2) = c("YEAR","Rain")
#Here I calculate the annual precipitation totals. I want to save the #results for the rain gauges that are in the columns, in the same #spreadsheet (see example below).
 PRCTOT=ddply(df2, .(YEAR), summarize, 
               Prec = sum(na.omit(Rain)));PRCTOT
  
  dfr$PRCTOT[cont]=PRCTOT

  cont = cont + 1
  
}

#Save tests results
library(writexl)
write_xlsx(dfr, "Results.xlsx")

我想要的数据框示例:

enter image description here

r dataframe loops time-series
1个回答
0
投票

不使用循环,可以使用

summarize
包中的函数
dplyr
(取代
plyr
)来计算一行中的总和:

library(dplyr)

dfr <- summarize(df, across(starts_with("RainGaue"), ~ sum(.x, na.rm = T)), .by = year)

给你数据框:

   year RainGaue1 RainGaue2 RainGaue3 RainGaue4
1  1976    1595.7    1595.7    1595.7    1595.7
2  1977    1263.4    1263.4    1263.4    1263.4
3  1978    1098.0    1098.0    1098.0    1098.0
4  1979    1130.0    1130.0    1130.0    1130.0
5  1980    1501.5    1501.5    1501.5    1501.5
6  1981    1224.6    1224.6    1224.6    1224.6
7  1982    1823.6    1823.6    1823.6    1823.6
8  1983    1753.8    1753.8    1753.8    1753.8
9  1984    1060.0    1060.0    1060.0    1060.0
10 1985     977.0     977.0     977.0     977.0
11 1986    1490.3    1490.3    1490.3    1490.3
12 1987    1255.9    1255.9    1255.9    1255.9
13 1988    1150.2    1150.2    1150.2    1150.2
14 1989    1490.9    1490.9    1490.9    1490.9
15 1990    1351.0    1351.0    1351.0    1351.0
16 1991    1027.0    1027.0    1027.0    1027.0
17 1992    1458.4    1458.4    1458.4    1458.4
18 1993    1211.2    1211.2    1211.2    1211.2
19 1994    1175.6    1175.6    1175.6    1175.6
20 1995    1509.0    1509.0    1509.0    1509.0
21 1996    1511.3    1511.3    1511.3    1511.3
22 1997    1693.7    1693.7    1693.7    1693.7
23 1998    1651.2    1651.2    1651.2    1651.2
24 1999    1540.2    1540.2    1540.2    1540.2
25 2000    1411.2    1411.2    1411.2    1411.2
26 2001    1119.5    1119.5    1119.5    1119.5
27 2002    1334.3    1334.3    1334.3    1334.3
28 2003    1022.0    1022.0    1022.0    1022.0
29 2004     996.4     996.4     996.4     996.4
30 2005    1417.0    1417.0    1417.0    1417.0
31 2006    1023.8    1023.8    1023.8    1023.8
32 2007    1415.5    1415.5    1415.5    1415.5
33 2008     921.0     921.0     921.0     921.0
34 2009    1503.5    1503.5    1503.5    1503.5
35 2010    1237.4    1237.4    1237.4    1237.4
© www.soinside.com 2019 - 2024. All rights reserved.