create data.frame where the output of the file is placed in columns

1

Hello! I have a problem with R .... I have a file called dados_dia with different meteorological variables: Índice de claridade (I.Cla), Wind speed (speed), Umidade relativa (hH) and Temperatura do ar (Tai) . I need to find the dates on which a series of combinations of these variables occur (144 combinations, to be more exact). For this I have a script in which I use a " for " to generate these combinations and to find the dates. Because each combination results in different date quantities, I can not write those dates in a vector or another file, just make a print . follows the script:

          I.Cla <- c(0,0.3,0.6,0.9)
          speed <- c(0,2,4,8)
          Tair <- c(0,14,19,24,40)
          rH <-  c(0,70,80,90,100)
combinacao = NULL

    m=1

          for(i in 2:4){
            for(j in 2:4){
              for(l in 2:5){
                for(k in 2:5){

        ######################################################################
        ############# condições das variáveis ################################

         combinacao[m] <- paste('I.Cla > ',I.Cla[i-1],' e I.Cla <= ',I.Cla[i],
                          'speed > ', speed[j-1],' e speed <= ',speed[j],
                          'Tair > ',Tair[l-1],' e Tair <= ',Tair[l], 
                          'rH > ',rH[k-1],' e rH <= ',rH[k] )

        m=m+1

        #####################################################################
        ############# datas em que ocorrem cada condição das variáveis ######

        print(c(dados_dia$date[dados_dia$I.Cla > I.Cla[i-1] & 
                               dados_dia$I.Cla <= I.Cla[i] &
                               dados_dia$speed > speed[j-1] &
                               dados_dia$speed <= speed[j] & 
                               dados_dia$Tair > Tair[l-1] & 
                               dados_dia$Tair <= Tair[l] &
                               dados_dia$rH > rH[k-1] &
                               dados_dia$rH <= rH[k]] ) )

                             }
                       }
                  } 
             } 

I need the conditions of the variables (combination) to be the columns of my data.frame and the dates generated in the print are arranged in the columns that match the same. Note: Results range from zero to 20 dates.

    
asked by anonymous 13.02.2018 / 18:45

1 answer

0

I got ... I created an array with the number of columns corresponding to the size of the vector combinacao[m] and turned it into a data.frame .

The problem I had was to enter the results of the commands executed within the print within each column, because these were different numbers of results. The solution was simple ......

Enter in the lines of my data.frame plus for with the output length of print . So, the different amount of results is deposited on each column ....

follow script:

tabelas <- matrix(nrow = 100 , ncol = length(combinacao) )
colnames(tabelas) <- combinacao

          tabelas <- data.frame(tabelas)
c=1

        for(i in 2:length(I.Cla)){
          for(j in 2:length(speed)){
            for(l in 2:length(Tair)){
              for(k in 2:length(rH)){ 

bla <- dados_dia$date[dados_dia$I.Cla > I.Cla[i-1] & 
       dados_dia$I.Cla <= I.Cla[i] &
       dados_dia$speed > speed[j-1] &
       dados_dia$speed <= speed[j] & 
       dados_dia$Tair > Tair[l-1] & 
       dados_dia$Tair <= Tair[l] &
       dados_dia$rH > rH[k-1] &
       dados_dia$rH <= rH[k] ] 

        for(m in 1:length(bla)){

 tabelas[m,c] <- as.character(bla[k])

        }

 c=c+1
                 }
                }
              }
           }
    
16.02.2018 / 20:05