[R] Select the rows in a dataframe that matches a criteria in another dataframe

David Winsemius dwinsemius at comcast.net
Mon May 11 14:36:04 CEST 2009


On May 10, 2009, at 7:54 PM, David Winsemius wrote:

>
> ?subset
> ?"%in%"
>
> (I have gotten tired of converting dataframes that are presented in  
> a non-executable form, such as is supported by the dput function.   
> So, ... you should read those help pages and take the obvious path  
> to success.)
>
> Something along the lines of:
>
> subset(df1, Firm %in% df2[df2$Audited==yes, "Firm"] )  #untested

Looking at my untested code I can see one error already. (Perhaps)  
should be:

subset(df1, Firm %in% df2[df2$Audited=="yes", "Firm"] )

>
> On May 10, 2009, at 5:52 PM, Cecilia Carmo wrote:
>
>> Hi everyone! Thank you for the help you have been given to me, and  
>> here I'm with another problem with my dataframes:
>> I have two dataframes (with much more observations), like these:
>> Dataframe1
>> Firm         Year      cash
>> 500400200    2007     100
>> 500400200    2006     200
>> 500400200    2005     400
>> 500400300    2007     300
>> 500400300    2006     240
>> 500400300    2005     120
>> 500400400    2007     340
>> 500400400    2006     890
>> 500400400    2005     250
>>
>> Dataframe 2
>> Firm            Audited     consolidate
>> 500400200     yes           no
>> 500400300     yes          yes
>> 500400400     no            no
>>
>> I want to make another dataframe equal to the dataframe1, but just  
>> with the firms «audited», or with the firms «audited» and  
>> «consolidate». For example, with the audited and consolidated, the  
>> output would be just firm 500400300, like this:
>> Firm         Year      cash
>> 500400300    2007     300
>> 500400300    2006     240
>> 500400300    2005     120
>>
>> I’ve tried intersect () but it gives me just the number of the  
>> firm, and it is not what I want. What I want is a dataframe with  
>> all the information, but just the firms that match my criteria.
>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list