[R] Odp: What do you think about my function?

Petr PIKAL petr.pikal at precheza.cz
Wed Jun 3 10:57:38 CEST 2009


Hi

without knowing what the function does and without some fake data 
available it is almost impossible to do evaluation.

Few comments

you apply some function to all rows of data frame hence think about apply 
instead for cycle
you check NA values, try to look at complete cases
your NA check in if clause is rather awkward if not wrong, use is.na()
why do you get rid of NA, most R functions can handle them quite smoothly, 
through their na.action or na.rm options.

Regards
Petr



r-help-bounces at r-project.org napsal dne 02.06.2009 14:35:00:

> 
> Hello,
> I want to know what do you think about my function. I know that it isn't
> briliant :/ but what do you think? What I should do that my function 
will be
> better? (now is very slow and not ideal, sometimes I also get a 
mistake!)
> 
> ########## My function  #############################################
> dzieci<-transform(dzieci, zywnosc=0)
> 
> zywnoscCalosc<- function( jedzenie, sklep, n1, n2, n3, n4, d1, d2, d3, 
d4 )
> { 
>    skl <- sklep
>    wynik <- vector() 
>    wynik <- jedzenie
> 
>    ndf <- data.frame(nn1=n1,nn2=n2,nn3=n3,nn4=n4)
>    ddf <- data.frame(dd1=d1,dd2=d2,dd3=d3,dd4=d4)
> 
>    for (i in 1:length(n1)){
> 
>       wekt_n = ndf[i,]
>       wekt_d = ddf[i,]
> 
>       wekt_n_ok = wekt_n[!is.na(wekt_n)]
>       wekt_n_ok = as.numeric(wekt_n_ok)
>       wekt_d_ok = wekt_d[!is.na(wekt_d)]
>       wekt_d_ok = as.numeric(wekt_d_ok)
> 
>       dl_n = length(wekt_n_ok)
>       dl_d = length(wekt_d_ok)
> 
>       if (skl[i] %in% NA){ wynik[i]=NA}
> 
>       else {wynik[i] =
> sum(((1*wekt_n_ok)+(3*wekt_n_ok))/((1*dl_n)+(3*dl_d)))}
>       }
> return (wynik)
> }
> 
> ### Call function ### 
> 
> dzieci$zywnosc <- zywnoscCalosc(dzieci$zywnosc, dzieci$sklepik,
> dzieci$sklslodycze, dzieci$sklnapojegazowane, dzieci$sklfastfood,
> dzieci$sklsloneprzekaski, dzieci$sklmlekoiproduktymleczne,
> dzieci$sklwodamineralnasoki, dzieci$sklkanapki, dzieci$sklwarzywaiowoce)
> 
> #####################################################################
> 
> And about mistake in my function:
> This is one row from my database where I get a mistake and I don't know 
why?
> 
> sklepik; sklslodycze; sklnapojegazowane;
>    1           1                      1
> sklmlekoiproduktymleczne; sklfastfood; sklwodamineralnasoki;
>            1                             1                    1
> sklsloneprzekaski; sklkanapki; sklwarzywaiowoce; 
>    1                         0                    1 
> 
> Afetr call my function I get as a result: 
> dzieci$zywnosc[7590]
> [1] 1
> But in my opinion I should get a value = 0.75 What is wrong?
> 
> 
> -- 
> View this message in context: 
http://www.nabble.com/What-do-you-think-about-
> my-function--tp23831687p23831687.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list