[R] indexing within a function

Berton Gunter gunter.berton at gene.com
Fri Jan 20 21:30:33 CET 2006


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: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Arianne Albert
> Sent: Friday, January 20, 2006 11:27 AM
> To: r-help at stat.math.ethz.ch
> 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: albert at zoology.ubc.ca
> http://www.zoology.ubc.ca/~albert
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list