[R] subsetting data-frame by vector of characters

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Fri Jun 13 16:25:02 CEST 2008


james perkins wrote:
> Hi,
>
> I have a very simple problem but I can't think how to solve it without
> using a for loop and creating a large logical vector. However given
> the nature of the problem I am sure there is a "1-liner" that could do
> the same thing much more efficiently.
>
> bascially I have a dataframe with characters in, eg
>
> >names.and.numbers
>
> (index)    Name    Fave.Number
> 1    John    7
> 2    Tony    12
> 3    Phil    14
> 4    Adam    22
> 5    Robert    23
>
>
> Now, imagine I have a vector of names, ie:
>
> >names = c("John,Phil,Robert")

this is a one-element vector of string(s) that are concatenated names
(strings with names).
or you mean:  names = c("John", "Phil", "Robert")


>
> All I want to do is get the subset of the dataframe which corresponds
> to the names in the vector "Names". IE
>
> (index)    Name    Fave.Number
> 1    John    7
> 2    Phil    14
> 3    Robert    23

this should do:
names.and.numbers[names.and.numbers$Name %in% names,]

if names is as you say above, do
names.and.numbers[names.and.numbers$Name %in% strsplit(names,","), ]

you do create a logical vector here (what does 'large' mean?), but no
loop is involved at the surface.

vQ



More information about the R-help mailing list