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

Asked

Viewed 112 times

1

Hello! I have a little problem in R.... I have a file called dados_dia with different weather variables: Índice de claridade (I.Cla),Wind speed (speed), Umidade relativa (hH)and Temperatura do ar (Tai). I need to find the dates when a series of combinations of these variables occur (144 combinations, to be more exact). For that I have a script in which I use a "for" to generate these combinations and to find the dates. Because each combination results in quantities of different dates, I’m not able to write these dates in one vector or another file, just make one 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 it. NOTE: The results range from zero to 20 dates.

  • 3

    Unfortunately, this question cannot be reproduced by anyone trying to answer it. Information about what is dados_dia. Please, take a look at this link and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.

  • I updated the question

  • The question is still not reproducible.

  • The problem is variable dados_dia. I suggest using the dputand post here the result.

  • Thank you for your attention !!!!

1 answer

0

I managed to... I created a matrix with the column number corresponding to the vector size combinacao[m] and turned it into a data.frame.

The problem I had was to introduce the results of the executed commands into the print within each column, as they were different numbers of results. The solution was simple......

Insert in the lines of my data.frame one more for with the output length of the print. Thus, the different amount of results is deposited in each column....

follows 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
                 }
                }
              }
           }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.