0

I have three lists of csv files which are 1 day, 2 day and 3 day forecasts of stations. I wrote 3 loops. Before run, I gave i=1, j=1 and k=1 to run a sample and see the result. The result dataframe getting out is different with for loop result. For loop result is totally wrong.

   rm(list = ls())

    options(stringsAsFactors = F)
    Combine_files1 = list.files(path = "D:/Raw_data/pdf/CPY/output_WL_1day/clear",
                        pattern = '*.csv')
    Combine_files2 = list.files(path = "D:/Raw_data/pdf/CPY/output_WL_2day/clear",
                        pattern = '*.csv')
    Combine_files3 = list.files(path = "D:/Raw_data/pdf/CPY/output_WL_3day/clear",
                        pattern = '*.csv')

    stn_name=substr(Combine_files1,1,6)
    cnt = rle(stn_name)

    for (i in  1 : length(Combine_files1)){
      setwd("D:/Raw_data/pdf/CPY/output_WL_1day/clear")
      df1 = read.csv(Combine_files1[i])
      df1$Date =  as.Date( df1$Date, '%m/%d/%Y')
      drops = c("X")
      df1 = df1[ , !(names(df1) %in% drops)]
      colnames(df1) = c("Date", "sim1","obs1")

    for (j in  1 : length(Combine_files2)){
        setwd("D:/Raw_data/pdf/CPY/output_WL_2day/clear")
        df2 = read.csv(Combine_files2[j])
        df2$Date =  as.Date( df2$Date, '%m/%d/%Y')
        drops = c("X")
        df2 = df2[ , !(names(df2) %in% drops)]
        colnames(df2) = c("Date", "sim2","obs2")


    for (k in  1 : length(Combine_files3)){
          setwd("D:/Raw_data/pdf/CPY/output_WL_3day/clear")
          df3 = read.csv(Combine_files3[k])
          df3$Date =  as.Date( df3$Date, '%m/%d/%Y')
          drops = c("X")
          df3 = df3[ , !(names(df3) %in% drops)]
          colnames(df3) = c("Date", "sim3","obs3")

  
          df4 = merge(df1, df2, by.x = "Date", by.y = "Date")
          df5 = merge(df4, df3, by.x = "Date", by.y = "Date")
          df= na.omit(df5)
  

        write.csv(df,file = paste("D:/Raw_data/pdf/CPY/all/"
                          ,cnt$values[i],"_all",
                            '.csv',sep = "")) 
    }
    }
    }

############################################################################ I got this result when giving i=1,j=1 and k= 1

    head(df)      
    Date     sim1   obs1     sim2   obs2     sim3   obs3
    1 2017-12-22 313.6230 314.39 313.6363 314.37 313.6230 314.39
    2 2017-12-24 313.6003 314.43 313.6242 314.31 313.6003 314.43
    3 2017-12-25 313.6089 314.31 313.6247 314.24 313.6089 314.31
    4 2017-12-26 313.5632 314.24 313.5847 314.20 313.5632 314.24
    5 2017-12-27 313.5233 314.20 313.6504 314.29 313.5233 314.20
    6 2017-12-28 313.4725 314.29 313.5836 314.54 313.4725 314.29

######################################################################## When run for loop and saving as csv file.I got this

enter image description here

  • 1) What were you expecting and what did you get? 2) You will get better answers if you write a reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Frank Jun 26 '20 at 03:41
  • Sorry, I added results just now. The problem is that when I giving i=1, j=1 and k=1, the result is correct but I run for all the list of files with for loop, the first file that came out is totally wrong. I cannot find the reason why it happens like that. – Kay Khaing Kyaw Jun 26 '20 at 03:58

0 Answers0