[R] truly object oriented programming in R Thank you all
Jason Liao
jg_liao at yahoo.com
Fri Aug 13 16:46:43 CEST 2004
My deep appreciation to everyone who responded to my question. I am
digesting your proposals (a little distracted by my governor's dramatic
resignation in New Jersey). It seems that I am able to implement the
algorithm in R (with the framework kindly provided by Thomas). I will
post the code when it is done. Currently, implemented in Java, the KD
tree kernel density algorithm can be an order faster than the double
loop brute force method. I will find out this will remain true in R.
Regards,
Jason
--- Thomas Lumley <tlumley at u.washington.edu> wrote:
> On Thu, 12 Aug 2004, Jason Liao wrote:
>
> > Dear Thomas,
> > Thank you very much again for taking time to answer my questions. I
> am
> > sorry that my knoweldge of R is limited as I have only learned what
> is
> > necessary to do my work. In the KD tree, we have this recursive
> data
> > structure in that each knod has two children knods and this process
> > continues until the data points are divided. Does R's list support
> this
> > recursive data structure? If yes, can you give a sample program?
>
> Yes, the elements of a list can be lists. For example, a simple
> binary
> tree could have lists with elements left, right, key, and data
>
> ## create a new single node
> newtree<-function(key,data){ list(left=NULL,right=NULL, key=key,
> data=data)}
>
> ## add a node to a sorted tree
> addnode<-function(tree, key, data){
>
> if (key<=tree$key){
> if (is.null(tree$left))
> tree$left<-newtree(data=data,key=key)
> else
> tree$left<-addnode(tree$left,key,data)
> } else {
> if (is.null(tree$right))
> tree$right<-newtree(data=data,key=key)
> else
> tree$right<-addnode(tree$left,key,data)
>
> }
> return(tree)
> }
>
>
> ## inorder traversal. action() is any function that takes key and
> data
> ## arguments
> applyinorder<-function(tree, action){
>
> c(if (!is.null(tree$left))
> applyinorder(tree$left,action),
> action(tree$key,tree$data),
> if (!is.null(tree$right))
> applyinorder(tree$right, action))
>
> }
>
>
> ## an example
> > a<-newtree("R","two optional method systems and first-class
> functions")
> > a<-addnode(a,"Java","compulsory object system")
> > a<-addnode(a,"C","No built-in support but that needn't stop you")
> > a<-addnode(a,"C++","If C++ is your hammer, everything looks like a
> thumb")
> > applyinorder(a,function(key,data) paste(key,data,sep=": "))
> [1] "C: No built-in support but that needn't stop you"
> [2] "C++: If C++ is your hammer, everything looks like a thumb"
> [3] "Java: compulsory object system"
> [4] "R: two optional method systems and first-class functions"
>
>
>
=====
Jason Liao, http://www.geocities.com/jg_liao
Dept. of Biostatistics, http://www2.umdnj.edu/bmtrxweb
University of Medicine and Dentistry of New Jersey
phone 732-235-5429, School of Public Health office
phone 732-235-8611, Cancer Institute of New Jersey office
moble phone 908-720-4205
More information about the R-help
mailing list