Hi, I try to make a function that have two others functions inside. It is simple, but the problem is that functions inside use the same variable, but with different values. I try something like this: Teste <- function(Pdig(nlinhas),Ldig(nlinhas)) { Pdig <- function(nlinhas) { Tdig <- (15.50 + 7.45*nlinhas); (3*(Tdig*(30/3600))+1*(30*(30/3600))); } Ldig <- function(nlinhas) { Tdig <- (30.50 + 7.45*nlinhas); (2*(Tdig*(30/3600))+1*(30*(30/3600))); } print(Pdig) print(Ldig) print(Pdig+Ldig) } It dont work. How to make this? Thanks Ronaldo -- If at first you don't succeed, quit; don't be a nut about success. -- | // | \\ [*****************************][*******************] || ( ? ? ) [Ronaldo Reis J?nior ][PentiumIII-600 ] | V [UFV/DBA-Entomologia ][HD: 30 + 10 Gb ] || / \ [36571-000 Vi?osa - MG ][RAM: 128 Mb ] | /(.''`.)\ [Fone: 31-3899-2532 ][Video: SiS620-8Mb ] ||/(: :' :)\ [chrysopa at insecta.ufv.br ][Modem: Pctel-onboar] |/ (`. `'` ) \[ICQ#: 5692561 ][Kernel: 2.4.18 ] || ( `- ) [*****************************][*******************] ||| _/ \_Powered by GNU/Debian W/Sarge D+ || Lxuser#: 205366
Ronaldo Reis Jr. wrote:> Hi, > > I try to make a function that have two others functions inside. > > It is simple, but the problem is that functions inside use the same variable, > but with different values. I try something like this: > > Teste <- function(Pdig(nlinhas),Ldig(nlinhas)) { > > Pdig <- function(nlinhas) { > Tdig <- (15.50 + 7.45*nlinhas); > (3*(Tdig*(30/3600))+1*(30*(30/3600))); > } > > Ldig <- function(nlinhas) { > Tdig <- (30.50 + 7.45*nlinhas); > (2*(Tdig*(30/3600))+1*(30*(30/3600))); > } > > print(Pdig) > print(Ldig) > print(Pdig+Ldig) > } > > It dont work. > > How to make this? > > Thanks > Ronaldo >Teste <- function(nlinhas) { Pdig <- function(z) { Tdig <- (15.50 + 7.45*z); (3*(Tdig*(30/3600))+1*(30*(30/3600))); } Ldig <- function(z) { Tdig <- (30.50 + 7.45*z); (2*(Tdig*(30/3600))+1*(30*(30/3600))); } print(Pdig(nlinhas)) print(Ldig(nlinhas)) print(Pdig(nlinhas)+Ldig(nlinhas)) } > Teste(10) [1] 2.5 [1] 2 [1] 4.5 >
Make it three separate functions. This gives an example of how argument passing works.> Pdig <- function(nlinhas) { > Tdig <- (15.50 + 7.45*nlinhas); > (3*(Tdig*(30/3600))+1*(30*(30/3600))); > }> Ldig <- function(nlinhas) { > Tdig <- (30.50 + 7.45*nlinhas); > (2*(Tdig*(30/3600))+1*(30*(30/3600))); > }> Teste <- function(nlinhas) { > print(Pdig(nlinhas)) > print(Ldig(nlinhas)) > print(Pdig(nlinhas) + Ldig(nlinhas)) > }> Teste(17)- tom blackwell - u michigan medical school - ann arbor -