[R] subsetting data-frame by vector of characters
Wacek Kusnierczyk
Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Fri Jun 13 16:54:11 CEST 2008
james perkins wrote:
> Thanks a lot for that. Its the %in% I needed to work out mainly
>
> large didn't mean anything in particular, just that it gets quite long
> with the real data.
> I did mean: names = c("John", "Phil", "Robert")
>
> The only problem is that using the method you suggest is that I lose
> the indexing, ie in the example, instead of:
>
> (index) Name Fave.Number
> 1 John 7
> 2 Phil 14
> 3 Robert 23
>
>
> I end up with
>
>
> (index) Name Fave.Number
> 1 John 7
> 3 Phil 14
> 5 Robert 23
>
> This isnt a problem at the moment but I guess it could be if I used
> the table later in loops. Is there an easy way to re-index the table?
strange. i run this simulated example, and it's ok:
d = data.frame(a=letters[rep(1:5,2)], b=letters[10:1])
d[d$a %in% letters[1:3], ]
you can always add an index column:
d = data.frame(index=1:dim(d)[[1]],d)
vQ
More information about the R-help
mailing list