青海SPEI计算

1. `library(lmomco)` 和 `library(SPEI)`: 这两行代码加载了需要用到的R包。`lmomco` 是一个用于使用L矩统计数据的包,而 `SPEI` 是一个用于计算标准化降水蒸散指数的包。

2. `setwd("D:/R/month/qinghai/")`: 这行代码将工作目录设置为指定路径,即Excel文件所在的路径。

3. `excel_files <- list.files(pattern = "\\.xlsx$")`: 这行代码创建了一个向量 `excel_files`,其中包含工作目录中所有以“.xlsx”结尾的Excel文件的名称。

4. `spei_results <- list()`: 这行代码创建了一个空列表 `spei_results`,用于存储每个Excel文件计算出的SPEI值。

5. `for (file in excel_files) { ... }`: 这个循环遍历 `excel_files` 向量中的每个Excel文件。

6. `changan <- read_excel(file)`: 这行代码将当前的Excel文件读入R,并将其存储在名为 `changan` 的数据框中。

7. `changan$PET <- thornthwaite(changan$TMED, 35.6957264957265)`: 这行代码使用Thornthwaite方法基于 `changan` 数据框中的温度数据 (`TMED`) 计算潜在蒸散发(PET)。

8. `spei1 <- spei(changan$PRCP - changan$PET, 1)`: 这行代码基于 `changan` 数据框中的降水减去PET数据 (`changan$PRCP - changan$PET`),以1个月的时间尺度(`1`)计算标准化降水蒸散指数(SPEI)。

9. `spei_results[[file]] <- spei1$fitted`: 这行代码使用当前Excel文件的名称作为键,将计算出的SPEI值存储在 `spei_results` 列表中。

10. `all_spei_results <- do.call(rbind, spei_results)`: 这行代码将 `spei_results` 列表中的所有SPEI结果合并成一个名为 `all_spei_results` 的数据框。

11. `write.csv(all_spei_results, file = "changanspei_1_combined.csv")`: 这行代码将合并后的SPEI结果写入一个名为“changanspei_1_combined.csv”的CSV文件中。

示例:

假设你有一个名为“data.xlsx”的Excel文件,其中包含“TMED”(温度)、“PRCP”(降水)列,以及上述代码在一个R脚本中。当你运行该脚本时,它会读取“data.xlsx”,计算PET,计算SPEI,并将结果存储在以“data.xlsx”为键的“spei_results”中。最后,它将所有SPEI结果合并成一个数据框,并保存为“changanspei_1_combined.csv”。

R语言代码:

library(lmomco)
library(SPEI)

# Set the working directory where your Excel files are located
setwd("D:/R/month/qinghai/")

# Get the list of Excel files in the directory
excel_files <- list.files(pattern = "\\.xlsx$")

# Create an empty list to store the results
spei_results <- list()

# Iterate over each Excel file
for (file in excel_files) {
  # Read the Excel file
  changan <- read_excel(file)
  
  # Perform calculations
  changan$PET <- thornthwaite(changan$TMED, 35.6957264957265)
  spei1 <- spei(changan$PRCP - changan$PET, 1)
  
  # Store the results in the list
  spei_results[[file]] <- spei1$fitted
  
  # Plot SPEI-12 (if needed)
  # plot(spei(ts(changan$PRCP - changan$PET, freq = 12, start = c(1964, 1)), 12),
  #      main = paste("SPEI-12 for", file))
}

# Combine all results into a single data frame
all_spei_results <- do.call(rbind, spei_results)

# Write the combined results to a CSV file
write.csv(all_spei_results, file = "changanspei_1_combined.csv")

library(lmomco)
library(SPEI)

# Set the working directory where your Excel files are located
setwd("D:/R/month/qinghai/")

# Get the list of Excel files in the directory
excel_files <- list.files(pattern = "\\.xlsx$")

# Create an empty list to store the results
spei_results <- list()

# Iterate over each Excel file
for (file in excel_files) {
  # Read the Excel file
  changan <- read_excel(file)
  
  # Perform calculations
  changan$PET <- thornthwaite(changan$TMED, 35.6957264957265)
  spei1 <- spei(changan$PRCP - changan$PET, 1)
  
  # Store the results in the list
  spei_results[[file]] <- spei1$fitted
  
  # Plot SPEI-12 (if needed)
  # plot(spei(ts(changan$PRCP - changan$PET, freq = 12, start = c(1964, 1)), 12),
  #      main = paste("SPEI-12 for", file))
}

# Combine all results into a single data frame
all_spei_results <- do.call(rbind, spei_results)

# Write the combined results to a CSV file
write.csv(all_spei_results, file = "changanspei_1_combined.csv")

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

___Y1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值