[R] Select cases of data.frame based on a long index

tsunhin wong thjwong at gmail.com
Thu Jun 4 00:21:29 CEST 2009


Dear R users,

I know how to select a subset of a data.frame or a matrix by setting
some criteria, but this time, I have a long index of cases not really
related to 1 or 2 criteria.
And I cannot find a simple way to select cases from another data.frame
based on this index (sublst in this example).
> sublst<-unique(g[comp.lst[[2]],]$Subject)
>sublst
 [1] 19 20 21 22 23 24 25 26 27 28 29 31 32 33 34 35 36 37 38 39 41 42
43 44 45 46 47 48 49
[30] 50 51 52 53 54 55 56 57 58 59 60 62 63 64
> foo.lst <- subset(gTrialList,(TrialList==1 & Subject==sublst))
Error:   longer object length is not a multiple of shorter object length

> foo.lst <- subset(gTrialList,(TrialList==1))
> foo.lst[foo.lst$Subject==sublst]
Error:
In foo.lst$Subject == sublst :
  longer object length is not a multiple of shorter object length

The only way I know is to use a for-loop to exhaust the index and then
combine the cases back together. But will there be a simpler way to do
so?

Thank you very much for the help!

- John




More information about the R-help mailing list