[R] dataframe subsetting

Thomas W Blackwell tblackw at umich.edu
Wed Sep 10 18:13:00 CEST 2003


Ryan  -

Puzzling.  My best guess is a misspelling or a truncation
in the column name "intent" within latdata in the context of

> unlikely <- latdata[latdata$intent %in% c(1,2,3,4),]

but I do not see any such misspelling in your email.  Could
you try again, using some other way to specify the column on
which you wish to subset ?  Such as: figure out what number
the column is (say it's the fifth column in latdata) and use

> unlikely <- latdata[latdata[[5]] %in% c(1,2,3,4),]

Otherwise, your R syntax looks fine to me.  Try also asking

> is.factor(latdata$intent)

but I don't think that would have caused the error message
you see.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -

On Wed, 10 Sep 2003, Ryan Thomas Moore wrote:

> I can create a small dataset, "x" below, and subset out rows based on
> values of a certain variable.  However, on the dataset I'm working on now,
> "latdata" below, I get a subscript error.  Any advice is appreciated!
>
> Ryan
>
> Successful:
>
> > is.data.frame(x)
> [1] TRUE
> > x
>   X1 X2 X3
> 1  1  3  5
> 2  2  4  6
> > x[x$X2 %in% c(3),]
>   X1 X2 X3
> 1  1  3  5
>
> Unsuccessful:
>
> > is.data.frame(latdata)
> [1] TRUE
> > is.numeric(latdata$intent)
> [1] TRUE
> > table(latdata$intent)
>
>   1   2   3   4   5   6
>  34  23  67 179 996   2
> > unlikely <- latdata[latdata$intent %in% c(1,2,3,4),]
> Error in x[[j]] : subscript out of bounds
> ------------------------------------------
> Ryan T. Moore ~ Government & Social Policy
> Ph.D. Candidate ~ Harvard University




More information about the R-help mailing list