[R] correct subset in R

David Winsemius dwinsemius at comcast.net
Thu Mar 23 20:58:43 CET 2017


> On Mar 23, 2017, at 11:28 AM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
> 
> Hello,
> 
> I would try ?merge, in particular see the arguments by.x and by.y
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Em 23-03-2017 18:19, Elahe chalabi via R-help escreveu:
>> Hi all,
>> I found an answer to the last question I asked in this group and now I want to have a correct subset of my data:
>> 
>> I have a following df1 which is list of all cities in US and their states:
>> 
>> $ name         : Factor w/ 1008 levels "Ackley","Ackworth",..: 1 2 3
>> $ state        : Factor w/ 1 level "Iowa": 1 1 1 1 1 1 1 1 1 1 .
>> 
>> the other data frame I have is df2 which is the list of cities I have and I want to get their state from df1:
>> 
>> $ cities : Factor w/ 547 levels "Afton","Boone","Calmar",...: 1 2 3
>> 
>> 
>> now how should I subset in a way to get related states of $cities from df1?

I think this is the proper use of the `match` function or its derivative `%in%`:

df1[ match(df2$cities, df1$name, no.match=0) , "state"]  # numeric indexing

df1[ df1$name %in% df2$cities , "state"] # logical indexing


Untested since the example is incomplete.

-- 
David

>> 
>> Thanks for any help!
>> Elahe
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>> 
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list