Thanks for your answer. But I have a strange question that I don't know how to explain and I really don't know how to spot the problematic part.
Suppose I have a long list of age, gender and bmi from a data.frame called msltdata. > age <- msltdata$age > gender <- msltdata$data > bmi <-msltdata$bmi > age [1] 5 10 14 > gender [1] 0 0 0 Levels: 0 1 > bmi [1] 17.17693 16.40702 16.87695 > mapply(bmisds,age,gender,bmi) however, when I try to create the vectors once again using the same data, i.e. > age <- c(5,10,14) > gender <- c(0,0,0) > bmi <- c(17.17693,16.40702,16.87695) > mapply(bmisds,age,gender,bmi) This time, I can get the correct calculation. Would anyone please give me some hints to find out what's went wrong? Regards, C On 10/3/07, Christos Hatzis <[EMAIL PROTECTED]> wrote: > Instead of > > > bmisds(age,gender,bmi) > > Try the vectorized version > > > mapply(bmisds, age, gender, bmi) > > See ?mapply > > -Christos > > > > > -----Original Message----- > > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] On Behalf Of Chung-hong Chan > > Sent: Wednesday, October 03, 2007 12:31 AM > > To: [EMAIL PROTECTED] > > Subject: [R] How to modify function for list > > > > Dear R Gurus, > > > > I have a function which calculate the BMI standard deviation > > score based on the age and gender specific L, M, S value. > > I have written a function like this > > > > bmisds <- function (age, gender, bmi){ > > if (age ==1 & gender ==1) > > { > > bmif <- c(-1.013,16.133,0.07656) > > } > > else if (age ==1 & gender ==0) > > { > > bmif <- c(-1.79,16.421,0.07149) > > } > > else if (age == 2 & gender == 1) > > { > > bmif <- c(-1.403,15.58,0.07342) > > } > > else if (age == 2 & gender == 0) > > { > > bmif <- c(-2.045,15.861,0.07116) > > } > > .... > > > > (((bmi/bmif[2])**bmif[1] -1)/(bmif[1]*bmif[3])) } > > > > This function work when I supply a single variable, e.g. > > bmisds(1,0,16) > > > > When I try it with a list, e.g. > > age <- c(12,13,14) > > gender <- c(0,1,1) > > bmi <- c(14,15,16) > > bmisds(age,gender,bmi) > > > > This function give me wrong answer and error msg. > > Warning messages: > > 1: the condition has length > 1 and only the first element > > will be used in: if (age == 1 & gender == 1) { > > > > > > How can I midify this function to work with list/vector? > > > > regards, > > C > > > > > > -- > > CH Chan > > Research Assistant - KWH > > http://www.macgrass.com > > > > ______________________________________________ > > R-help@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. > > > > > > > -- CH Chan Research Assistant - KWH http://www.macgrass.com ______________________________________________ R-help@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.