年度标准化降水指数 (SPI) - 使用 precintcon R 包的多个列

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

如何使用 precintcon R 包同时计算和保存多列的年度 SPI?我正在使用循环,但无法保存或提取列中雨量计的年度值。 我想保存为分析的每一年的所有雨量计(以列形式)计算的 SPI。

library(precintcon)

#Creating a data.frame exemple using precintcon package
df=melt(daily,id = c("year","month"))
df$value2=1.2*df$value;df$value3=2+(df$value)*5;df$value4=3.9*df$value;df$value5=(df$value)*10;df$value6=df$value
colnames(df) <- c('year','month','day','RainGauge1','RainGauge2','RainGaue3','RainGauge4','RainGaue5','RainGauge6') 
head(df)

#Looping
ncol(df)
nproc = 6
cont = 1

for (i in 4:8) {
  
  print(i) 
  df2<-df[,-3]#
  nome.var=colnames(df2[i])
  variavela = nome.var[1]

  TIM = df2[,c(1,2,i)]
  TIM = na.omit(TIM)
  names(TIM) = c("Year","Month","Cd")
  head(TIM)
  df2=TIM
  #Here I calculate the annual SPI (standard precipitation index). 
  #I want to save the #results for the rain gauges that are in the columns, #in the same spreadsheet (see example below).
  names(df2)[1] <- "year"
  names(df2)[2] <- "month"
  names(df2)[3] <- "precipitation"
  
  df2=as.monthly(df2)
  head(df2)
  
  SPI=spi.per.year(df2, period = 12);SPI
  
  cont = cont + 1
  
}



我想要所有雨量计列的结果如下。 我尝试了命令:

summarize(df, across(starts_with("RainGauge")
...但没有成功。

enter image description here

dataframe loops package rstudio
1个回答
0
投票

这肯定不漂亮,但我们可以将

apply
函数应用于
spi.per.year
的每一列。由于
df
需要特定的、笨拙的数据格式,因此它会变得混乱。
precintcon

	
© www.soinside.com 2019 - 2024. All rights reserved.