[R] subset in dataframes

Cecilia Carmo cecilia.carmo at ua.pt
Sun Oct 2 16:24:48 CEST 2011


Sarah,

Sorry for being ignorant. I was doing something wrong. It works perfectly. 

Thank you.

Cecília Carmo


-----Mensagem original-----
De: Sarah Goslee [mailto:sarah.goslee at gmail.com] 
Enviada: domingo, 2 de Outubro de 2011 14:21
Para: Cecilia Carmo
Cc: r-help at r-project.org
Assunto: Re: [R] subset in dataframes

Hi,

On Sun, Oct 2, 2011 at 9:08 AM, Cecilia Carmo <cecilia.carmo at ua.pt> wrote:
> Thank you very much.
>
> My dataframe has thousands of firms, how can I delete all of those with
x<0
> and keep another dataframe with firms where all x>0?

How does that differ from your original question? What doesn't work for you
in
the answer I already gave?

Sarah

> Thank you again.
>
> Cecília Carmo
> (Universidade de Aveiro - Portugal)
>
> -----Mensagem original-----
> De: Sarah Goslee [mailto:sarah.goslee at gmail.com]
> Enviada: domingo, 2 de Outubro de 2011 13:01
> Para: Cecilia Carmo
> Cc: r-help at r-project.org
> Assunto: Re: [R] subset in dataframes
>
> Hi,
>
> On Sun, Oct 2, 2011 at 7:48 AM, Cecilia Carmo <cecilia.carmo at ua.pt> wrote:
>> I need help in subseting a dataframe:
>>
>>
>>
>> data1<-data.frame(year=c(2001,2002,2003,2004,2001,2002,2003,2004,
>>
>> 2001,2002,2003,2004,2001,2002,2003,2004),
>>
>> firm=c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4),x=c(11,22,-32,25,-26,47,85,98,
>>
>> 101,14,87,56,12,43,67,54),
>>
>> y=c(110,220,302,250,260,470,850,980,1010,140,870,560,120,430,670,540))
>
>
> Thank you for providing a reproducible example.
>
>>
>>
>> data1
>>
>>
>>
>> I want to keep the firms where all x>0 (where there are no negative
values
>> in x)
>>
>> So my output should be:
>>
>>   year firm   x    y
>>
>> 1  2001    3 101 1010
>>
>> 2 2002    3  14  140
>>
>> 3 2003    3  87  870
>>
>> 4 2004    3  56  560
>>
>> 5 2001    4  12  120
>>
>> 6 2002    4  43  430
>>
>> 7 2003    4  67  670
>>
>> 8 2004    4  54  540
>>
>>
>>
>> So I'm doing:
>>
>> data2<-data1[data1$firm%in%subset(data1,data1$x>0),]
>>
>> data2
>>
>
>
> What about finding which ones have negative values and should be deleted,
>
>> unique(data1$firm[data1$x <= 0])
> [1] 1 2
>
> And then deleting them?
>
>> data1[!(data1$firm %in% unique(data1$firm[data1$x <= 0])),]
>   year firm   x    y
> 9  2001    3 101 1010
> 10 2002    3  14  140
> 11 2003    3  87  870
> 12 2004    3  56  560
> 13 2001    4  12  120
> 14 2002    4  43  430
> 15 2003    4  67  670
> 16 2004    4  54  540
>
>
>>
>> But the result is
>>
>> [1] year firm x    y
>>
>> <0 rows> (or 0-length row.names)
>>
>
>
> If you look at just the result of part of your code,
> subset(data1,data1$x>0)
> it isn't giving at all what you need for the next step: the entire
> data frame for x>0.
>



-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list