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.