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

Bert Gunter gunter.berton at gene.com
Thu Apr 24 02:32:47 CEST 2008


ummmm...

One of the S language's great strengths is the variety and flexibility of
it's data structures. It pays to familiarize yourself with them. So if x is
already a dataframe ("dataset" is a rather unhelpful description), then
row.names(x) <- x$label 
x[c("fun","food"),] 

will extract the rows you want (to be suitably assigned by you, of course).
Incidentally, the row names typically could have been set when you first
read the data in (e.g. via ?read.table() and friends).

If x is not a dataframe, then your should make it one, and ?data.frame will
tell you how, depending on what it actually is.

I repeat: learn about R's data structures and how to use them if you wish to
take full advantage of the language and its power (and avoid the
unnecessary, though correct (mod the typo), manipulations described in the
reply below).

-- Bert Gunter



-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Jorge Ivan Velez
Sent: Wednesday, April 23, 2008 4:48 PM
To: Dirkheld
Cc: r-help at r-project.org
Subject: Re: [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 at 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-string
s-tp16832735p16832735.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.
>

	[[alternative HTML version deleted]]

______________________________________________
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.



More information about the R-help mailing list