[R] Clever R syntax for extracting a subset of observations

Ko-Kang Kevin Wang k.wang at auckland.ac.nz
Mon Apr 12 06:23:01 CEST 2004

----- Original Message ----- 
From: "Ajay Shah" <ajayshah at mayin.org>
> But this doesn't work since if D is a data frame, you can't say
> D[d]. Let me show you:
> > x = runif(100)
> > y = runif(100)
> > D = data.frame(x, y)
> > d = c(7,3,2)
> > E = D[d]
> Error in "[.data.frame"(D, d) : undefined columns selected
> Any suggestions on how one can do such pretty things as D[d] where D
> is a data frame?

So I take it you want to select the 7th, 3rd, and 2nd observation (i.e.
rows) from D?

A data frame has two dimensions, so you need to specify the row and columns.
Maybe try:
  E = D[d,]


Kevin Wang

More information about the R-help mailing list