[R] indexing within a function

Arianne Albert albert at zoology.ubc.ca
Fri Jan 20 20:26:51 CET 2006


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: albert at zoology.ubc.ca
http://www.zoology.ubc.ca/~albert




More information about the R-help mailing list