[R] Subsetting without partial matches

Jonathan Dushoff jdushoff at gmail.com
Fri Jan 30 13:46:45 CET 2009


I have a list of observations of individuals.  I would like to make a 
list of individuals, with a data frame of observations for each individual.

The following code usually works, but not always

----------------------------------------------------------------------

# Make a list of empty data frames
animals = list()
indivs = levels(Z$individual_id)
donotprint <- sapply(indivs, function(i){
   animals[[i]] = data.frame()
})

# Add observations of each animal to the appropriate frame
donotprint <- apply(Z, 1, function(r){
   ind = r[["individual_id"]]
   bind = ind # Use different names to confirm that the partial matching 
is being done on the left

   animals[[bind]]$sighting_number <<-
      c(animals[[ind, exact=TRUE]]$sighting_number, r[["sighting_number"]])
   animals[[bind]]$date<<-
      c(animals[[ind, exact=TRUE]]$date, r[["date"]])
   animals[[bind]]$age <<-
      c(animals[[ind, exact=TRUE]]$age, r[["age_num"]])
})

----------------------------------------------------------------------

The problem is partial matching.  When it gives the wrong answer, it 
gives partial match warnings.  Adding "exact=TRUE" to the left, the way 
that I added it to the right, simply produces an argument error.  
Changing to single brackets produces other errors.

I read the help, and the Language Definition (not the whole thing), but 
could not find clear documentation of what single brackets with 
character variable arguments are supposed to do in lists, nor of how 
partial matching is handled on the left side of an assignment, nor of 
whether R is supposed to do partial-match indexing when an exact match 
is available (I would have thought not, and it's documented that it's 
not supposed to for function arguments).

I am interested in how the subsetting is supposed to work, but even more 
in what might be the best way to code this sort of thing in R.

I am using R 2.6.2 on Mandriva linux.

Thanks for any help,

JD




More information about the R-help mailing list