[R] selecting rows based on number that occurs after letter

Daisy Englert Duursma daisy.duursma at gmail.com
Tue Apr 27 07:10:45 CEST 2010


Thanks for your help, I have played around with the suggestion a bit
but I can still not sub-setting in the way I need

If I have a matrix x as follows:

  > x <- matrix(c("BA1y1","BA2y3","C3A1r1y1","C3A2r2y2t4","C3r2y1y1",1,2,3,
, nrow=5, ncol=2,
dimnames=list(c("a","b","c","d","e"), c("GCM","y")))
  > x
  GCM          y
a "BA1y1"      "1"
b "BA2y3"      "2"
c "C3A1r1y1"   "3"
d "C3A2r2y2t4" "11"
e "C3r2t1y1"   "12"

and I want to loop through 3 subsets based on the numeric value after
the y, how do I do this?

What I currently have is:
> year <-c("1", "2", "3")
> for (y in year) {
> subx <- x[sapply(strsplit(as.character(x$GCM), ""), function(zzz)zzz[5] == y),]
Basically this loops through and subsets the rows when the 5th
character has the defined y value (1, 2,or 3). The problem is that y
can occur anywhere in the GCM value.

Thanks for the help.
Daisy Englert Duursma

Room E8C156
Dept. Biological Sciences
Macquarie University  NSW  2109

Tel +61 2 9850 9256

More information about the R-help mailing list