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
Sarah Goslee http://www.functionaldiversity.org