Skip to content
Prev 275890 / 398506 Next

Extracting data by row

You didn't show the details of what you did before
   Ten<-dataTable1[(dataTable1$sensor_depth_m=="10"),]
but that line makes me suspicious that you did some
experimentation with syntax before coming up with that
line.  In particular, why did you use parentheses around
   (dataTable1$sensor_depth_m=="10")
and why did you use quotes around the 10?

Here is an example of why you might have started using
the unnecessary (and misleading) parentheses.  I'll
use a narrow dataset so its R printout does not get
line-wrapped by a rogue mailer.

  > d <- data.frame(one=c(3,1,1,2), ten=c(3,1,1,2)*10)
  > # first, use = instead of == for comparison
  > d[ d$one = 1, ]
  Error: unexpected '=' in "d[ d$one ="
  > # react to error message by adding parentheses
  > d[ (d$one = 1), ]
    one ten
  1   3  30
  > # That is definitely the wrong answer.
  > # The parentheses hid that problem that you were using =
  > # instead of == and did d$one<-1, then d[1,] returned row 1.
  > # Note that now d has been changed!
  > d
    one ten
  1   1  30
  2   1  10
  3   1  10
  4   1  20

That may explain how you got a column of 10's (after a ...=10),
but I don't know what you may have done to get a second column
called sensor_depth_m.  Nor do I have a guess as to why you
put quotes around the number 10.

This is why R-help asks to see exactly what you did in R,
not just a synopsis.  (When I copied what you showed into
R, adding a read.csv(header=TRUE, textConnection("...data...")),
I got the correct 2 lines of output, not the incorrect 3 lines you
indicated that you wanted.)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com