[R] Subsetting without partial matches
    Jonathan Dushoff 
    jdushoff at gmail.com
       
    Sat Jan 31 09:02:56 CET 2009
    
    
  
David:
Thank you for your very valuable response.  In fact, I was trying to 
_avoid_ partial matching, not accomplish it.  Subset is a _much_ better 
way of doing what I was trying to do.
Humorously, however, your code also reproduces the mistake that brought 
me here, AFAICT.  I think my code behaved weirdly because of my use of = 
instead of <<- inside sapply.
With subset, we can avoid that choice altogether.  My new code, which 
appears to work, is:
===
animals <- sapply(unique(Z$id), function(i){
   subset(Z, id==i, select=c(sighting_number, date, age_num))
}, simplify=FALSE)
===
Why should I consider renaming the date column?
----------------------------------------------------------------------
I have not seen you describe the value of doing partial matching in  
this application, so pardon this perhaps non-responsive reply:  
Wouldn't it have been much, much simpler to have used the subset  
function (which returns a dataframe object) at the first assignment to  
donotprint?
Something along the lines of  (untested) :
 > donotprint <- sapply(indivs, function(i){
 >  animals[[i]] = subset(Z, individual_id == i, select =  
 > c(sighting_number, date, age_num) ) }  # reconsider naming variable  
 > "date"
                       )
    
    
More information about the R-help
mailing list