Dirkheld
2008-Apr-23 07:13 UTC
[R] select rows from data based on a vector of char strings
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-tp16832735p16832735.html Sent from the R help mailing list archive at Nabble.com.
Chuck Cleland
2008-Apr-23 08:24 UTC
[R] select rows from data based on a vector of char strings
On 4/23/2008 3:13 AM, 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?newdata <- subset(data, label %in% flist) ?subset ?is.element -- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Applejus
2008-Apr-23 23:41 UTC
[R] select rows from data based on a vector of char strings
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.
Jorge Ivan Velez
2008-Apr-23 23:48 UTC
[R] select rows from data based on a vector of char strings
Try this, x="label freq1 freq2 news 54 35 fun 37 21 milk 19 7 food 3 3" yourdata=read.table(textConnection(x),header=TRUE) attached(yourdata) flist<-c("fun","food") yourdata[label %in% flist,] I hope this helps, Jorge On Wed, Apr 23, 2008 at 3:13 AM, Dirkheld <dirk.bollen@soc.kuleuven.be> 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-tp16832735p16832735.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]