Re: [R] select rows from data based on a vector of char strings

From: Applejus <ielkhoury_at_gmail.com>
Date: Wed, 23 Apr 2008 16:41:33 -0700 (PDT)

Hi,

You are right the == doesn't work, but there's a workaround using regular expressions:

flist<-"fun|food"
grep(flist, data$label)

will give you the vector [2 4] which are the numbers of the rows of interest!

Dirkheld wrote:
>
> Hi,
>
> I have loaded a dataset in R :
> data =
>
> label freq1 freq2
> news 54 35
> fun 37 21
> milk 19 7
> food 3 3
> .... etc
>
> And I have a vector
> flist<-c("fun","food")
>
> Now I want to use the vector 'flist' for selecting these values from
> 'data' so that I get the following dataset :
> label freq1 freq2
> fun 37 21
> food 3 3
>
> When I do 'data$label==flist[1]' I get 'F T F F', so it works for one item
> in the char vector flist.
> But, when I do 'data$label==flist' I get 'F F F F' while I expected 'F T F
> T'. It seems that I can't perform this action with a vector of
> charstrings?
>
> Is there an other way to do so?
>
>

-- 
View this message in context: http://www.nabble.com/select-rows-from-data-based-on-a-vector-of-char-strings-tp16832735p16834876.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 23 Apr 2008 - 23:43:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 24 Apr 2008 - 10:30:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive