[R] subset problem

reeyarn reeyarn at gmail.com
Thu Mar 29 07:25:45 CEST 2012


Hi,

If my data frame "df" has a index "type", I want to get a subset such
that the type belongs to a "type_list";
using sql, I want
  SELECT name, type FROM df
    WHERE type in type_list;

Now in R I have to write a loop like
  mysubset<- df [ df$type == type_list[1], ]
  for (type1 in type_list[ 2: length (type_list) ] ) {
    mysubset<-cbind (mysubset, df [ df$type == type1, ])
  }

What is the natural way of doing this in R? Is it possible to use
subset() to attain this?
Thanks!

Best,
Reeyarn

On Fri, Dec 3, 2010 at 11:26 AM,  William Dunlap <[hidden email]> wrote:
> HI,
> I have a dataframe like this:
> name    type
> A          t1
> B           t2
> C          t1
> D           t4
> E           t3
> F            t2
> how can I have a "sub dataframe" based with the column "type" like this:
> (for type = t1)
> name    type
> A          t1
> C          t1
> D           t1

Hi,

Let's say your data.frame is stored in a variable named "df":

R> subset(df, type == 't1')

Read the help files:

R> ?subset

Also take a look at ?split

-steve



More information about the R-help mailing list