[R] subset problem
peter dalgaard
pdalgd at gmail.com
Thu Mar 29 07:44:55 CEST 2012
On Mar 29, 2012, at 07:25 , reeyarn wrote:
> 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, ])
> }
>
(that should be rbind(), I believe)
> What is the natural way of doing this in R? Is it possible to use
> subset() to attain this?
%in% is your friend
mysub <- subset(df, type %in% type_list, select=c(name,type))
or
mysub <- df[df$type %in% type_list, c("name","type")]
The latter is slightly safer if you can't be sure that df doesn't contain a column called type_list.
> 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
>
> ______________________________________________
> 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.
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list