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