Language R - Use of function to prevent code repetitions

Asked

Viewed 98 times

2

Staff I am developing a program in R language, the same compares data from two data.frame and writes in a third, the program presents several conditional structures of the type else if within two repeating structures of the type for and if any of these is satisfied the data is recorded in this third data.frame. The part of the code that saves the data in the third data.frame if repeated several times during the code I would like to know if there is any way to create a type of function that I can pass the parameters so that it makes the recording without having to repeat it every time? In the simplified example below I put only 3 conditions, but in the real program are 13, for this example the part that repeats is always:

  tabela3$coluna1[k]<-tabela2$coluna1[j]
  tabela3$coluna2[k]<-tabela2$coluna2[j]
  tabela3$coluna3[k]<-tabela2$coluna3[j]
  tabela3$coluna4[k]<-tabela2$coluna4[j]
  tabela3$coluna5[k]<-tabela2$coluna5[j]

Obs: I tried to create a function, but it didn’t work!

k<-1
for(i in 1:nrow(tabela1)){
    for(j in 1:nrow(tabela2)){      
      if (condicao 1 for satisfeia){  

      tabela3$coluna1[k]<-tabela2$coluna1[j]
      tabela3$coluna2[k]<-tabela2$coluna2[j]
      tabela3$coluna3[k]<-tabela2$coluna3[j]
      tabela3$coluna4[k]<-tabela2$coluna4[j]
      tabela3$coluna5[k]<-tabela2$coluna5[j]
       k<- k+1      
    }

      else if(condicao 2 for satisfeita){
      tabela3$coluna1[k]<-tabela2$coluna1[j]
      tabela3$coluna2[k]<-tabela2$coluna2[j]
      tabela3$coluna3[k]<-tabela2$coluna3[j]
      tabela3$coluna4[k]<-tabela2$coluna4[j]
      tabela3$coluna5[k]<-tabela2$coluna5[j]
      k<-k+1 
    }
    else if(condicao 3 for satisfeita){         
      tabela3$coluna1[k]<-tabela2$coluna1[j]
      tabela3$coluna2[k]<-tabela2$coluna2[j]
      tabela3$coluna3[k]<-tabela2$coluna3[j]
      tabela3$coluna4[k]<-tabela2$coluna4[j]
      tabela3$coluna5[k]<-tabela2$coluna5[j]

      k<-k+1       
    }        
}

Thank you.

2 answers

2

I don’t know how flexible you want at the level of the two data frames tabela3 and tabela2, but from what I understand you don’t want to repeat the following code:

tabela3$coluna1[k]<-tabela2$coluna1[j]
tabela3$coluna2[k]<-tabela2$coluna2[j]
tabela3$coluna3[k]<-tabela2$coluna3[j]
tabela3$coluna4[k]<-tabela2$coluna4[j]
tabela3$coluna5[k]<-tabela2$coluna5[j]

I imagine that the function you tried to create (it would be better if you had provided the question) did not work because functions in R work in different environments. In order for the steps taken in a function to be recognized outside the function, you need to assign to all environments (global Environment).

To assign an object in a global environment, you can simply use <<-:

fun <- function() {
  tabela3$coluna1[k] <<- tabela2$coluna1[j]
  tabela3$coluna2[k] <<- tabela2$coluna2[j]
  tabela3$coluna3[k] <<- tabela2$coluna3[j]
  tabela3$coluna4[k] <<- tabela2$coluna4[j]
  tabela3$coluna5[k] <<- tabela2$coluna5[j]
}

However, the use of <<- is not recommended as it increases the possibility of side effects. More details on this here (page 35). An alternative is the use of the function assign():

Instead of

tabela3$coluna1[k] <<- tabela2$coluna1[j]

You use

assign(tabela3$coluna1[k], tabela2$coluna1[j], envir = .GlobalEnv)

Those were the two ways I found it, but I’m curious if anyone has anything easier and less dangerous.

  • I tested both suggestions, but the only one that worked was this "<<-", but as it was said can cause side effects! I get a little frustrated with R by the difficulty of solving a problem that is simple to solve in other languages. Thank you.

  • What was the error message for the second option? If you put part of your data would be easier to help: dput(head(tabela2)) and dput(head(tabela3)).

  • This is the code I just modified the name of the tables and columns, but the structure is the same. Thank you

0

k<-1
for(i in 1:nrow(tabela1)){
  print(k)
  for(j in 1:nrow(tabela2)){


    if(is.na(tabela1$col1[i]) && tabela1$col2[i]=="xx" && isTRUE(tabela2$col3[j]==tabela1$col3[i]) && tabela2$col4[j]==1 && tabela2$col5[j]==1 && isTRUE(tabela1$col6[i]>=tabela2$col6[j])){

      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])
    }

    else if(is.na(tabela1$col1[i]) && tabela2$col4[j]==1 && tabela2$col5[j]==0 && isTRUE(tabela1$col6[i]>=tabela2$col6[j])){

      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 

    }

    else if(isTRUE(tabela1$col1[i]=="2") && isTRUE(tabela2$col1[j]=="2") && tabela2$col4[j]==1 && isTRUE(1-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="2.1") && isTRUE(tabela2$col1[j]=="2.1") && tabela2$col4[j]==1 && isTRUE(1-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="3") && isTRUE(tabela2$col1[j]=="3") && tabela2$col4[j]==1 && isTRUE(1-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="3.1") && isTRUE(tabela2$col1[j]=="3.1") && tabela2$col4[j]==1 && isTRUE(1-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="4") && isTRUE(tabela2$col1[j]=="4") && tabela2$col4[j]==1 && isTRUE(6-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="4.1") && isTRUE(tabela2$col1[j]=="4.1") && tabela2$col4[j]==1 && isTRUE(6-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="5") && isTRUE(tabela2$col1[j]=="5") && tabela2$col4[j]==1 && isTRUE(6-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="5.1") && isTRUE(tabela2$col1[j]=="5.1") && tabela2$col4[j]==1 && isTRUE(6-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="6") && isTRUE(tabela2$col1[j]=="6") && tabela2$col4[j]==1 && isTRUE(20-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="6.1") && isTRUE(tabela2$col1[j]=="6.1") && tabela2$col4[j]==1 && isTRUE(20-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="7") && isTRUE(tabela2$col1[j]=="7") && tabela2$col4[j]==1 && isTRUE(20-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="7.1") && isTRUE(tabela2$col1[j]=="7.1") && tabela2$col4[j]==1 && isTRUE(20-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="8") && isTRUE(tabela2$col1[j]=="8") && tabela2$col4[j]==1 && isTRUE(20-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="8.1") && isTRUE(tabela2$col1[j]=="8.1") && tabela2$col4[j]==1 && isTRUE(20-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 

    }

    else if(isTRUE(tabela1$col1[i]=="9") && isTRUE(tabela2$col1[j]=="9") && tabela2$col4[j]==1 && isTRUE(20-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }


    else if(isTRUE(tabela1$col1[i]=="9.1") && isTRUE(tabela2$col1[j]=="9.1") && tabela2$col4[j]==1 && isTRUE(20-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="10") && isTRUE(tabela2$col1[j]=="10") && tabela2$col4[j]==1 && isTRUE(20-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="11") && isTRUE(tabela2$col1[j]=="11") && tabela2$col4[j]==1 && isTRUE(40-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }

    else if(isTRUE(tabela1$col1[i]=="11.1") && isTRUE(tabela2$col1[j]=="11.1") && tabela2$col4[j]==1 && isTRUE(40-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }



    else if(isTRUE(tabela1$col1[i]=="12") && isTRUE(tabela2$col1[j]=="12") && tabela2$col4[j]==1 && isTRUE(40-tabela1$col8[i]>0)){


      tabela1$col6[i]<-tabela1$col6[i]-1
      tabela1$col1[i]<-as.character(tabela2$col1[j])
      tabela2$col4[j]<-0

      tabela3$col12Def[k]<-as.character(tabela1$col11[i])
      tabela3$col10Def[k]<-as.character(tabela2$col10[j])
      tabela3$col8Def[k]<-as.character(tabela1$col8[i])
      tabela3$col1Def[k]<-as.character(tabela2$col1[j])
      tabela3$col3[k]<-as.character(tabela2$col3[j])
      tabela3$col9[k]<-as.character(tabela1$col9[i])
      tabela3$col2[k]<-as.character(tabela1$col2[i])

      k<-k+1 


    }



    tabela1$col8<- tabela1$col6-tabela1$col6
  }

}

Browser other questions tagged

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