Hmmm.. no one's responded yet... **Please** Read An Introduction to R and the R Language Definition to learn how to write functions and specify arguments.
Lose the quotes around x and y in your function. x is a name. "x" is a character. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box > -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Arianne Albert > Sent: Friday, January 20, 2006 11:27 AM > To: [email protected] > Subject: [R] indexing within a function > > Hello all, > > I've got a large set of data consisting of 2 continuous numerical > variables, and 2 factors. I'm trying to write a function that will > draw scatter plots of the 2 numerical variables for various > combinations of the factors. The problem is that my function doesn't > seem to understand what I want it to do even though the > command works > fine outside the function. Here is an example of what I'm > trying to do: > > > time<-seq(1, 10, 1) > > depth<-seq(10, 19, 1) > > pop<-rep(1:5, times=2) > > pop<-as.factor(pop) > > id<-rep(c(6,7), times=5) > > id<-as.factor(id) > > > mydf<-cbind(time,depth,pop,id) > > mydf<-as.data.frame(mydf) > > attach(mydf) > > > myfunc<-function(x, y){ #this is the type of function that's not > working > + xx<-time[pop=="x"&id=="y"] > + print(xx)} > > > myfunc(1,6) > numeric(0) > > But, if I just enter: > >time[pop=="1"&id=="6"] > [1] 1 > > I get the right answer... Does anyone know why the indexing works > outside the function, but not inside? > > Thanks in advance, > > Arianne Albert > _______________________ > Arianne Albert (PhD candidate) > Zoology Dept, UBC > 6270 University Blvd > Vancouver, BC, V6T 1Z4, Canada > Ph: 604-822-5966 > email: [EMAIL PROTECTED] > http://www.zoology.ubc.ca/~albert > > ______________________________________________ > [email protected] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
