[R] What do you think about my function?
Grześ
gregorio99 at gmail.com
Tue Jun 2 14:35:00 CEST 2009
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.
More information about the R-help
mailing list