[R] problem selecting rows meeting a criterion
Nordlund, Dan (DSHS/RDA)
NordlDJ at dshs.wa.gov
Tue Aug 11 02:13:56 CEST 2009
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> Behalf Of Jim Bouldin
> Sent: Monday, August 10, 2009 4:44 PM
> To: John Kane; r-help at r-project.org
> Subject: Re: [R] problem selecting rows meeting a criterion
>
>
> What's wrong is I'm trying to select only those rows in which X > Y, but
> I'm getting rows in which Y > X and losing some in which X > Y. The row
> numbers are not being read as values. Very confusing.
> Jim
> >
> > What's wrong with it? It looks okay to me. If you use
> > subset(data, data$X >data$Y)you get the same results. Any chance you're
> > reading the row.numbers as values?
> >
> > BTW "data" is a reserved word in R and it is good practice not to use it
> > as a variable name.
> >
> > My Results
> >
> > X Y V3
> > 3 3 1 2.236068
> > 4 4 1 6.324555
> > 5 5 1 5.000000
> > 6 1 2 8.062258
> > 10 5 2 5.656854
> > 11 1 3 2.236068
> > 12 2 3 9.486833
> > 17 2 4 2.236068
> > 18 3 4 8.062258
> > 24 4 5 5.385165
> >
> >
> > --- On Mon, 8/10/09, Jim Bouldin <jrbouldin at ucdavis.edu> wrote:
> >
> > > From: Jim Bouldin <jrbouldin at ucdavis.edu>
> > > Subject: [R] problem selecting rows meeting a criterion
> > > To: r-help at r-project.org
> > > Received: Monday, August 10, 2009, 5:49 PM
> > >
> > > When I try to select only those rows from the following
> > > data frame, called
> > > "data", in which X > Y
> > >
> > > X Y V3
> > > 2 2 1 8.062258
> > > 3 3 1 2.236068
> > > 4 4 1 6.324555
> > > 5 5 1 5.000000
> > > 6 1 2 8.062258
> > > 8 3 2 9.486833
> > > 9 4 2 2.236068
> > > 10 5 2 5.656854
> > > 11 1 3 2.236068
> > > 12 2 3 9.486833
> > > 14 4 3 8.062258
> > > 15 5 3 5.099020
> > > 16 1 4 6.324555
> > > 17 2 4 2.236068
> > > 18 3 4 8.062258
> > > 20 5 4 5.385165
> > > 21 1 5 5.000000
> > > 22 2 5 5.656854
> > > 23 3 5 5.099020
> > > 24 4 5 5.385165
> > >
> > > using the commands
> > > > attach(data)
> > > > data2 = data[X >Y,];data2
> > >
> > > I get this for data2:
> > >
> > > X Y V3
> > > 3 3 1 2.236068
> > > 4 4 1 6.324555
> > > 5 5 1 5.000000
> > > 6 1 2 8.062258
> > > 10 5 2 5.656854
> > > 11 1 3 2.236068
> > > 12 2 3 9.486833
> > > 17 2 4 2.236068
> > > 18 3 4 8.062258
> > > 24 4 5 5.385165
> > >
> > > Clearly, this is not what I intend but I cannot figure out
> > > what I've done
> > > wrong. Any help appreciated. Thanks.
> > >
> > > Jim Bouldin
> >
> >
Jim,
I copied your data to a text file and read it in using read.table(). Your code worked as expected for me. So, your problem is not reproducible. Is it possible that your data frame has been corrupted? What does str(data) show you?
Dan
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
More information about the R-help
mailing list