Language R - Use of function to avoid repetitions of code

1
I'm developing a program in the R language, it compares data of two data.frame and writes to a third, the program has several conditional structures of type else if within two repeat structures of type for , being if any of these are satisfied the data is recorded in this third data.frame . The part of the code that writes the data to the third% w / o is repeated several times during the code I would like to know if there is any way to create a function type so that I can pass the parameters so that it can write without need to repeat it every time? In the simplified example below I only put 3 conditions, but in the actual program they are 13, for this example the repeating part 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 did not 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.

    
asked by anonymous 09.04.2018 / 23:28

2 answers

1

I do not know how flexible you want at the level of the two data frames tabela3 and tabela2 , but from what I understand you do not 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 have been better if you had made it available in the question) did not work because functions in R work in different environments . In order for the steps performed in a function to be recognized outside of the function, you must assign it 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 about this here (page 35). An alternative is the use of the assign() function:

Instead of

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

You use

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

These were the two ways I found it, but I'm curious if anyone has something easier and less dangerous.

    
10.04.2018 / 13:21
1

k

10.04.2018 / 16:21