删除循环

时间:2014-03-20 12:17:40

标签: r if-statement for-loop

我正在尝试运行一个我写过的函数但是我意识到它只需要太长时间。任何人都可以告诉我在以下函数中删除i for循环的最佳方法。该功能旨在读取大量现场天气数据,然后找出特定日期超过该月平均极端天气的天气。

我试图使用ifelse但是失败了。

     Extremes <- function (siteno){
 for (j in siteno){

  # reads in the weather data for the specipied site
  Butterfly_data <- read.csv(paste("~/Project Data/Site Subsets/SITE", j, ".csv", sep = ""))
  Precip <- read.csv(paste("//ueahome5/ressci5/zuw13bqu/data/NTProfile/Desktop/EOBS European data/SiteWeather/rr/Weather.Site.", j, ".csv", sep = ""))
  Tmin <- read.csv(paste("//ueahome5/ressci5/zuw13bqu/data/NTProfile/Desktop/EOBS European data/SiteWeather/tn/Weather.Site.", j, ".csv", sep = ""))
  Tmax <- read.csv(paste("//ueahome5/ressci5/zuw13bqu/data/NTProfile/Desktop/EOBS European data/SiteWeather/tx/Weather.Site.", j, ".csv", sep = ""))


  Precip[Precip[,1] < -900,1] <- NA
  Tmax[Tmax[,1] < -90, 1] <- NA
  Tmin[Tmin[,1] < -90, 1] <- NA

  z <- merge(Precip, Tmax, by.x = "Date", by.y = "Date") # merges the weather data into one dataframe 
  SiteWeather <- merge(z, Tmin, by.x = "Date", by.y = "Date" ) 
  SiteWeather[,1] <- as.Date(SiteWeather[,1], "%d/%m/%Y")

  SiteWeather[,5] <- as.numeric(format(SiteWeather[,1], "%m")) # takes out the month value from the Date so as to be able to assess means by month and create seasonal means throughout the dataset


  JanWeather<- subset(SiteWeather, SiteWeather[,5] == 1)#split the weather into months
  FebWeather<- subset(SiteWeather, SiteWeather[,5] == 2)
  MarWeather<- subset(SiteWeather, SiteWeather[,5] == 3)
  AprWeather<- subset(SiteWeather, SiteWeather[,5] == 4)
  MayWeather<- subset(SiteWeather, SiteWeather[,5] == 5)
  JunWeather<- subset(SiteWeather, SiteWeather[,5] == 6)
  JulWeather<- subset(SiteWeather, SiteWeather[,5] == 7)
  AugWeather<- subset(SiteWeather, SiteWeather[,5] == 8)
  SepWeather<- subset(SiteWeather, SiteWeather[,5] == 9)
  OctWeather<- subset(SiteWeather, SiteWeather[,5] == 10)
  NovWeather<- subset(SiteWeather, SiteWeather[,5] == 11)
  DecWeather<- subset(SiteWeather, SiteWeather[,5] == 12)

  JanExtPrecip <- mean(JanWeather[,2], na.rm = TRUE) + sd(JanWeather[,2], na.rm = TRUE)  
  JanExtTmax <- mean(JanWeather[,3], na.rm = TRUE) + sd(JanWeather[,3], na.rm = TRUE)
  JanExtTmin <- mean(JanWeather[,4], na.rm = TRUE) - sd(JanWeather[,4], na.rm = TRUE)
  FebExtPrecip <- mean(FebWeather[,2], na.rm = TRUE) + sd(FebWeather[,2], na.rm = TRUE)  
  FebExtTmax <- mean(FebWeather[,3], na.rm = TRUE) + sd(FebWeather[,3], na.rm = TRUE)
  FebExtTmin <- mean(FebWeather[,4], na.rm = TRUE) - sd(FebWeather[,4], na.rm = TRUE) 
  MarExtPrecip <- mean(MarWeather[,2], na.rm = TRUE) + sd(MarWeather[,2], na.rm = TRUE)  
  MarExtTmax <- mean(MarWeather[,3], na.rm = TRUE) + sd(MarWeather[,3], na.rm = TRUE)
  MarExtTmin <- mean(MarWeather[,4], na.rm = TRUE) - sd(MarWeather[,4], na.rm = TRUE)
  AprExtPrecip <- mean(AprWeather[,2], na.rm = TRUE) + sd(AprWeather[,2], na.rm = TRUE)  
  AprExtTmax <- mean(AprWeather[,3], na.rm = TRUE) + sd(AprWeather[,3], na.rm = TRUE)
  AprExtTmin <- mean(AprWeather[,4], na.rm = TRUE) - sd(AprWeather[,4], na.rm = TRUE)  
  MayExtPrecip <- mean(MayWeather[,2], na.rm = TRUE) + sd(MayWeather[,2], na.rm = TRUE)  
  MayExtTmax <- mean(MayWeather[,3], na.rm = TRUE) + sd(MayWeather[,3], na.rm = TRUE)
  MayExtTmin <- mean(MayWeather[,4], na.rm = TRUE) - sd(MayWeather[,4], na.rm = TRUE)
  JunExtPrecip <- mean(JunWeather[,2], na.rm = TRUE) + sd(JunWeather[,2], na.rm = TRUE)  
  JunExtTmax <- mean(JunWeather[,3], na.rm = TRUE) + sd(JunWeather[,3], na.rm = TRUE)
  JunExtTmin <- mean(JunWeather[,4], na.rm = TRUE) - sd(JunWeather[,4], na.rm = TRUE)
  JulExtPrecip <- mean(JulWeather[,2], na.rm = TRUE) + sd(JulWeather[,2], na.rm = TRUE)  
  JulExtTmax <- mean(JulWeather[,3], na.rm = TRUE) + sd(JulWeather[,3], na.rm = TRUE)
  JulExtTmin <- mean(JulWeather[,4], na.rm = TRUE) - sd(JulWeather[,4], na.rm = TRUE)
  AugExtPrecip <- mean(AugWeather[,2], na.rm = TRUE) + sd(AugWeather[,2], na.rm = TRUE)  
  AugExtTmax <- mean(AugWeather[,3], na.rm = TRUE) + sd(AugWeather[,3], na.rm = TRUE)
  AugExtTmin <- mean(AugWeather[,4], na.rm = TRUE) - sd(AugWeather[,4], na.rm = TRUE)
  SepExtPrecip <- mean(SepWeather[,2], na.rm = TRUE) + sd(SepWeather[,2], na.rm = TRUE)  
  SepExtTmax <- mean(SepWeather[,3], na.rm = TRUE) + sd(SepWeather[,3], na.rm = TRUE)
  SepExtTmin <- mean(SepWeather[,4], na.rm = TRUE) - sd(SepWeather[,4], na.rm = TRUE)
  OctExtPrecip <- mean(OctWeather[,2], na.rm = TRUE) + sd(OctWeather[,2], na.rm = TRUE)  
  OctExtTmax <- mean(OctWeather[,3], na.rm = TRUE) + sd(OctWeather[,3], na.rm = TRUE)
  OctExtTmin <- mean(OctWeather[,4], na.rm = TRUE) - sd(OctWeather[,4], na.rm = TRUE)
  NovExtPrecip <- mean(NovWeather[,2], na.rm = TRUE) + sd(NovWeather[,2], na.rm = TRUE)  
  NovExtTmax <- mean(NovWeather[,3], na.rm = TRUE) + sd(NovWeather[,3], na.rm = TRUE)
  NovExtTmin <- mean(NovWeather[,4], na.rm = TRUE) - sd(NovWeather[,4], na.rm = TRUE)
  DecExtPrecip <- mean(DecWeather[,2], na.rm = TRUE) + sd(DecWeather[,2], na.rm = TRUE)  
  DecExtTmax <- mean(DecWeather[,3], na.rm = TRUE) + sd(DecWeather[,3], na.rm = TRUE)
  DecExtTmin <- mean(DecWeather[,4], na.rm = TRUE) - sd(DecWeather[,4], na.rm = TRUE)






  for(i in 1:length(SiteWeather[,1])) {
    if(SiteWeather[i,5]== 1 ){
      SiteWeather[i,6] <- 1*(SiteWeather[i,2] >= JanExtPrecip) #checks whether the daily value exceeds the daily extreme for the average extreme of each month
      SiteWeather[i,7] <- 1*(SiteWeather[i,3] >= JanExtTmax)
      SiteWeather[i,8] <- 1*(SiteWeather[i,4] <= JanExtTmin)
    }
    if(SiteWeather[i,5]== 2 ){
      SiteWeather[i,6] <- 1*(SiteWeather[i,2] >= FebExtPrecip) #checks whether the daily value exceeds the daily extreme
      SiteWeather[i,7] <- 1*(SiteWeather[i,3] >= FebExtTmax)
      SiteWeather[i,8] <- 1*(SiteWeather[i,4] <= FebExtTmin)
    }  
    if(SiteWeather[i,5]== 3 ){
      SiteWeather[i,6] <- 1*(SiteWeather[i,2] >= MarExtPrecip) #checks whether the daily value exceeds the daily extreme
      SiteWeather[i,7] <- 1*(SiteWeather[i,3] >= MarExtTmax)
      SiteWeather[i,8] <- 1*(SiteWeather[i,4] <= MarExtTmin)
    }
    if(SiteWeather[i,5]== 4 ){
      SiteWeather[i,6] <- 1*(SiteWeather[i,2] >= AprExtPrecip) #checks whether the daily value exceeds the daily extreme
      SiteWeather[i,7] <- 1*(SiteWeather[i,3] >= AprExtTmax)
      SiteWeather[i,8] <- 1*(SiteWeather[i,4] <= AprExtTmin)
    }  
    if(SiteWeather[i,5]== 5 ){
      SiteWeather[i,6] <- 1*(SiteWeather[i,2] >= MayExtPrecip) #checks whether the daily value exceeds the daily extreme
      SiteWeather[i,7] <- 1*(SiteWeather[i,3] >= MayExtTmax)
      SiteWeather[i,8] <- 1*(SiteWeather[i,4] <= MayExtTmin)
    }  
    if(SiteWeather[i,5]== 6 ){
      SiteWeather[i,6] <- 1*(SiteWeather[i,2] >= JunExtPrecip) #checks whether the daily value exceeds the daily extreme
      SiteWeather[i,7] <- 1*(SiteWeather[i,3] >= JunExtTmax)
      SiteWeather[i,8] <- 1*(SiteWeather[i,4] <= JunExtTmin)
    }  
    if(SiteWeather[i,5]== 7 ){
      SiteWeather[i,6] <- 1*(SiteWeather[i,2] >= JulExtPrecip) #checks whether the daily value exceeds the daily extreme
      SiteWeather[i,7] <- 1*(SiteWeather[i,3] >= JulExtTmax)
      SiteWeather[i,8] <- 1*(SiteWeather[i,4] <= JulExtTmin)
    }  
    if(SiteWeather[i,5]== 8 ){
      SiteWeather[i,6] <- 1*(SiteWeather[i,2] >= AugExtPrecip) #checks whether the daily value exceeds the daily extreme
      SiteWeather[i,7] <- 1*(SiteWeather[i,3] >= AugExtTmax)
      SiteWeather[i,8] <- 1*(SiteWeather[i,4] <= AugExtTmin)
    }  
    if(SiteWeather[i,5]== 9 ){
      SiteWeather[i,6] <- 1*(SiteWeather[i,2] >= SepExtPrecip) #checks whether the daily value exceeds the daily extreme
      SiteWeather[i,7] <- 1*(SiteWeather[i,3] >= SepExtTmax)
      SiteWeather[i,8] <- 1*(SiteWeather[i,4] <= SepExtTmin)
    }  
    if(SiteWeather[i,5]== 10 ){
      SiteWeather[i,6] <- 1*(SiteWeather[i,2] >= OctExtPrecip) #checks whether the daily value exceeds the daily extreme
      SiteWeather[i,7] <- 1*(SiteWeather[i,3] >= OctExtTmax)
      SiteWeather[i,8] <- 1*(SiteWeather[i,4] <= OctExtTmin)
    }  
    if(SiteWeather[i,5]== 11 ){
      SiteWeather[i,6] <- 1*(SiteWeather[i,2] >= NovExtPrecip) #checks whether the daily value exceeds the daily extreme
      SiteWeather[i,7] <- 1*(SiteWeather[i,3] >= NovExtTmax)
      SiteWeather[i,8] <- 1*(SiteWeather[i,4] <= NovExtTmin)
    }  
    if(SiteWeather[i,5]== 12 ){
      SiteWeather[i,6] <- 1*(SiteWeather[i,2] >= DecExtPrecip) #checks whether the daily value exceeds the daily extreme
      SiteWeather[i,7] <- 1*(SiteWeather[i,3] >= DecExtTmax)
      SiteWeather[i,8] <- 1*(SiteWeather[i,4] <= DecExtTmin)
    }  

    print(i)
  }
  colnames(SiteWeather)<- c("Date", "Precip", "Tmax", "Tmin","Month" ,"ExtPrecip", "ExtTmax", "ExtTmin" )
  SiteWeather<- SiteWeather[order(as.Date(SiteWeather[,1])),]
 write.table(SiteWeather, paste("Site.", j, ".csv", sep = ",", row.names = FALSE))
 print(j)
 }
}

这是我尝试使用ifelse:

Extremes<- function (siteno){
  for (j in siteno){

    # reads in the weather data for the specipied site
    Butterfly_data <- read.csv(paste("~/Project Data/Site Subsets/SITE", j, ".csv", sep = ""))
    Precip <- read.csv(paste("//ueahome5/ressci5/zuw13bqu/data/NTProfile/Desktop/EOBS European data/SiteWeather/rr/Weather.Site.", j, ".csv", sep = ""))
    Tmin <- read.csv(paste("//ueahome5/ressci5/zuw13bqu/data/NTProfile/Desktop/EOBS European data/SiteWeather/tn/Weather.Site.", j, ".csv", sep = ""))
    Tmax <- read.csv(paste("//ueahome5/ressci5/zuw13bqu/data/NTProfile/Desktop/EOBS European data/SiteWeather/tx/Weather.Site.", j, ".csv", sep = ""))


    Precip[Precip[,1] < -900,1]<- NA
    Tmax[Tmax[,1] < -90, 1]<- NA
    Tmin[Tmin[,1] < -90, 1]<- NA

    z<- merge(Precip, Tmax, by.x = "Date", by.y = "Date") # merges the weather data into one dataframe 
    SiteWeather <- merge(z, Tmin, by.x = "Date", by.y = "Date" ) 
    SiteWeather[,1] <- as.Date(SiteWeather[,1], "%d/%m/%Y")

    SiteWeather[,5] <- as.numeric(format(SiteWeather[,1], "%m")) # takes out the month value from the Date so as to be able to assess means by month and create seasonal means throughout the dataset

    JanWeather<- subset(SiteWeather, SiteWeather[,5] == 1)#split the weather into months
    FebWeather<- subset(SiteWeather, SiteWeather[,5] == 2)
    MarWeather<- subset(SiteWeather, SiteWeather[,5] == 3)
    AprWeather<- subset(SiteWeather, SiteWeather[,5] == 4)
    MayWeather<- subset(SiteWeather, SiteWeather[,5] == 5)
    JunWeather<- subset(SiteWeather, SiteWeather[,5] == 6)
    JulWeather<- subset(SiteWeather, SiteWeather[,5] == 7)
    AugWeather<- subset(SiteWeather, SiteWeather[,5] == 8)
    SepWeather<- subset(SiteWeather, SiteWeather[,5] == 9)
    OctWeather<- subset(SiteWeather, SiteWeather[,5] == 10)
    NovWeather<- subset(SiteWeather, SiteWeather[,5] == 11)
    DecWeather<- subset(SiteWeather, SiteWeather[,5] == 12)

    JanExtPrecip <- mean(JanWeather[,2], na.rm = TRUE) + sd(JanWeather[,2], na.rm = TRUE)  
    JanExtTmax <- mean(JanWeather[,3], na.rm = TRUE) + sd(JanWeather[,3], na.rm = TRUE)
    JanExtTmin <- mean(JanWeather[,4], na.rm = TRUE) - sd(JanWeather[,4], na.rm = TRUE)
    FebExtPrecip <- mean(FebWeather[,2], na.rm = TRUE) + sd(FebWeather[,2], na.rm = TRUE)  
    FebExtTmax <- mean(FebWeather[,3], na.rm = TRUE) + sd(FebWeather[,3], na.rm = TRUE)
    FebExtTmin <- mean(FebWeather[,4], na.rm = TRUE) - sd(FebWeather[,4], na.rm = TRUE) 
    MarExtPrecip <- mean(MarWeather[,2], na.rm = TRUE) + sd(MarWeather[,2], na.rm = TRUE)  
    MarExtTmax <- mean(MarWeather[,3], na.rm = TRUE) + sd(MarWeather[,3], na.rm = TRUE)
    MarExtTmin <- mean(MarWeather[,4], na.rm = TRUE) - sd(MarWeather[,4], na.rm = TRUE)
    AprExtPrecip <- mean(AprWeather[,2], na.rm = TRUE) + sd(AprWeather[,2], na.rm = TRUE)  
    AprExtTmax <- mean(AprWeather[,3], na.rm = TRUE) + sd(AprWeather[,3], na.rm = TRUE)
    AprExtTmin <- mean(AprWeather[,4], na.rm = TRUE) - sd(AprWeather[,4], na.rm = TRUE)  
    MayExtPrecip <- mean(MayWeather[,2], na.rm = TRUE) + sd(MayWeather[,2], na.rm = TRUE)  
    MayExtTmax <- mean(MayWeather[,3], na.rm = TRUE) + sd(MayWeather[,3], na.rm = TRUE)
    MayExtTmin <- mean(MayWeather[,4], na.rm = TRUE) - sd(MayWeather[,4], na.rm = TRUE)
    JunExtPrecip <- mean(JunWeather[,2], na.rm = TRUE) + sd(JunWeather[,2], na.rm = TRUE)  
    JunExtTmax <- mean(JunWeather[,3], na.rm = TRUE) + sd(JunWeather[,3], na.rm = TRUE)
    JunExtTmin <- mean(JunWeather[,4], na.rm = TRUE) - sd(JunWeather[,4], na.rm = TRUE)
    JulExtPrecip <- mean(JulWeather[,2], na.rm = TRUE) + sd(JulWeather[,2], na.rm = TRUE)  
    JulExtTmax <- mean(JulWeather[,3], na.rm = TRUE) + sd(JulWeather[,3], na.rm = TRUE)
    JulExtTmin <- mean(JulWeather[,4], na.rm = TRUE) - sd(JulWeather[,4], na.rm = TRUE)
    AugExtPrecip <- mean(AugWeather[,2], na.rm = TRUE) + sd(AugWeather[,2], na.rm = TRUE)  
    AugExtTmax <- mean(AugWeather[,3], na.rm = TRUE) + sd(AugWeather[,3], na.rm = TRUE)
    AugExtTmin <- mean(AugWeather[,4], na.rm = TRUE) - sd(AugWeather[,4], na.rm = TRUE)
    SepExtPrecip <- mean(SepWeather[,2], na.rm = TRUE) + sd(SepWeather[,2], na.rm = TRUE)  
    SepExtTmax <- mean(SepWeather[,3], na.rm = TRUE) + sd(SepWeather[,3], na.rm = TRUE)
    SepExtTmin <- mean(SepWeather[,4], na.rm = TRUE) - sd(SepWeather[,4], na.rm = TRUE)
    OctExtPrecip <- mean(OctWeather[,2], na.rm = TRUE) + sd(OctWeather[,2], na.rm = TRUE)  
    OctExtTmax <- mean(OctWeather[,3], na.rm = TRUE) + sd(OctWeather[,3], na.rm = TRUE)
    OctExtTmin <- mean(OctWeather[,4], na.rm = TRUE) - sd(OctWeather[,4], na.rm = TRUE)
    NovExtPrecip <- mean(NovWeather[,2], na.rm = TRUE) + sd(NovWeather[,2], na.rm = TRUE)  
    NovExtTmax <- mean(NovWeather[,3], na.rm = TRUE) + sd(NovWeather[,3], na.rm = TRUE)
    NovExtTmin <- mean(NovWeather[,4], na.rm = TRUE) - sd(NovWeather[,4], na.rm = TRUE)
    DecExtPrecip <- mean(DecWeather[,2], na.rm = TRUE) + sd(DecWeather[,2], na.rm = TRUE)  
    DecExtTmax <- mean(DecWeather[,3], na.rm = TRUE) + sd(DecWeather[,3], na.rm = TRUE)
    DecExtTmin <- mean(DecWeather[,4], na.rm = TRUE) - sd(DecWeather[,4], na.rm = TRUE)






    #checks whether the daily value exceeds the daily extreme for each month
    ifelse(SiteWeather[,5]== 1 & SiteWeather[,2] >= JanExtPrecip,SiteWeather[,6] <- 1, SiteWeather[,6]<- 0)
    ifelse(SiteWeather[,5]== 1 & SiteWeather[,3] >= JanExtTmax,SiteWeather[,7] <- 1, SiteWeather[,7]<- 0)   
    ifelse(SiteWeather[,5]== 1 & SiteWeather[,4] <= JanExtTmin,SiteWeather[,8] <- 1, SiteWeather[,8]<- 0)

    ifelse(SiteWeather[,5]== 2 & SiteWeather[,2] >= FebExtPrecip,SiteWeather[,6] <- 1, SiteWeather[,6]<- 0)
    ifelse(SiteWeather[,5]== 2 & SiteWeather[,3] >= FebExtTmax,SiteWeather[,7] <- 1, SiteWeather[,7]<- 0)   
    ifelse(SiteWeather[,5]== 2 & SiteWeather[,4] <= FebExtTmin,SiteWeather[,8] <- 1, SiteWeather[,8]<- 0)

    ifelse(SiteWeather[,5]== 3 & SiteWeather[,2] >= MarExtPrecip,SiteWeather[,6]<- 1, SiteWeather[,6]<- 0)
    ifelse(SiteWeather[,5]== 3 & SiteWeather[,3] >= MarExtTmax,SiteWeather[,7]<- 1, SiteWeather[,7]<- 0)   
    ifelse(SiteWeather[,5]== 3 & SiteWeather[,4] <= MarExtTmin,SiteWeather[,8]<- 1, SiteWeather[,8]<- 0)

    ifelse(SiteWeather[,5]== 4 & SiteWeather[,2] >= AprExtPrecip,SiteWeather[,6]<- 1, SiteWeather[,6]<- 0)
    ifelse(SiteWeather[,5]== 4 & SiteWeather[,3] >= AprExtTmax,SiteWeather[,7]<- 1, SiteWeather[,7]<- 0)   
    ifelse(SiteWeather[,5]== 4 & SiteWeather[,4] <= AprExtTmin,SiteWeather[,8]<- 1, SiteWeather[,8]<- 0)

    ifelse(SiteWeather[,5]== 5 & SiteWeather[,2] >= MayExtPrecip,SiteWeather[,6]<- 1, SiteWeather[,6]<- 0)
    ifelse(SiteWeather[,5]== 5 & SiteWeather[,3] >= MayExtTmax,SiteWeather[,7]<- 1, SiteWeather[,7]<- 0)   
    ifelse(SiteWeather[,5]== 5 & SiteWeather[,4] <= MayExtTmin,SiteWeather[,8]<- 1, SiteWeather[,8]<- 0)

    ifelse(SiteWeather[,5]== 6 & SiteWeather[,2] >= JunExtPrecip,SiteWeather[,6]<- 1, SiteWeather[,6]<- 0)
    ifelse(SiteWeather[,5]== 6 & SiteWeather[,3] >= JunExtTmax,SiteWeather[,7]<- 1, SiteWeather[,7]<- 0)   
    ifelse(SiteWeather[,5]== 6 & SiteWeather[,4] <= JunExtTmin,SiteWeather[,8]<- 1, SiteWeather[,8]<- 0)

    ifelse(SiteWeather[,5]== 7 & SiteWeather[,2] >= JulExtPrecip,SiteWeather[,6]<- 1, SiteWeather[,6]<- 0)
    ifelse(SiteWeather[,5]== 7 & SiteWeather[,3] >= JulExtTmax,SiteWeather[,7]<- 1, SiteWeather[,7]<- 0)   
    ifelse(SiteWeather[,5]== 7 & SiteWeather[,4] <= JulExtTmin,SiteWeather[,8]<- 1, SiteWeather[,8]<- 0)

    ifelse(SiteWeather[,5]== 8 & SiteWeather[,2] >= AugExtPrecip,SiteWeather[,6]<- 1, SiteWeather[,6]<- 0)
    ifelse(SiteWeather[,5]== 8 & SiteWeather[,3] >= AugExtTmax,SiteWeather[,7]<- 1, SiteWeather[,7]<- 0)   
    ifelse(SiteWeather[,5]== 8 & SiteWeather[,4] <= AugExtTmin,SiteWeather[,8]<- 1, SiteWeather[,8]<- 0)

    ifelse(SiteWeather[,5]== 9 & SiteWeather[,2] >= SepExtPrecip,SiteWeather[,6]<- 1, SiteWeather[,6]<- 0)
    ifelse(SiteWeather[,5]== 9 & SiteWeather[,3] >= SepExtTmax,SiteWeather[,7]<- 1, SiteWeather[,7]<- 0)   
    ifelse(SiteWeather[,5]== 9 & SiteWeather[,4] <= SepExtTmin,SiteWeather[,8]<- 1, SiteWeather[,8]<- 0)

    ifelse(SiteWeather[,5]== 10 & SiteWeather[,2] >= OctExtPrecip,SiteWeather[,6]<- 1, SiteWeather[,6]<- 0)
    ifelse(SiteWeather[,5]== 10 & SiteWeather[,3] >= OctExtTmax,SiteWeather[,7]<- 1, SiteWeather[,7]<- 0)   
    ifelse(SiteWeather[,5]== 10 & SiteWeather[,4] <= OctExtTmin,SiteWeather[,8]<- 1, SiteWeather[,8]<- 0)

    ifelse(SiteWeather[,5]== 11 & SiteWeather[,2] >= NovExtPrecip,SiteWeather[,6]<- 1, SiteWeather[,6]<- 0)
    ifelse(SiteWeather[,5]== 11 & SiteWeather[,3] >= NovExtTmax,SiteWeather[,7]<- 1, SiteWeather[,7]<- 0)   
    ifelse(SiteWeather[,5]== 11 & SiteWeather[,4] <= NovExtTmin,SiteWeather[,8]<- 1, SiteWeather[,8]<- 0)

    ifelse(SiteWeather[,5]== 12 & SiteWeather[,2] >= DecExtPrecip,SiteWeather[,6]<- 1, SiteWeather[,6]<- 0)
    ifelse(SiteWeather[,5]== 12 & SiteWeather[,3] >= DecExtTmax,SiteWeather[,7]<- 1, SiteWeather[,7]<- 0)   
    ifelse(SiteWeather[,5]== 12 & SiteWeather[,4] <= DecExtTmin,SiteWeather[,8]<- 1, SiteWeather[,8]<- 0)


    colnames(SiteWeather)<- c("Date", "Precip", "Tmax", "Tmin","Month" ,"ExtPrecip", "ExtTmax", "ExtTmin" )
    SiteWeather<- SiteWeather[order(as.Date(SiteWeather[,1])),]
    write.csv(SiteWeather, paste("Site.", j, ".csv", sep = ""), sep = ",", row.names = FALSE)
  }

  print(j)

}

只是想加快我的功能,因为有很多网站数据需要阅读。

0 个答案:

没有答案