Skip to content

Extracting data by row

3 messages · Vinny Moriarty, Rainer Schuermann, William Dunlap

#
On Friday 28 October 2011 18:04:59 Vinny Moriarty wrote:
Here is what I would do, based on my understanding of your question:

# your data snippet as data frame
site          time_local            time_utc reef_type_code sensor_type
1    6 2006-04-09 10:20:00 2006-04-09 20:20:00            BAK        sb39
2    6 2006-04-09 10:40:00 2006-04-09 20:40:00            BAK        sb39
3    6 2006-04-09 11:00:00 2006-04-09 21:00:00            BAK        sb39
4    6 2006-04-09 11:20:00 2006-04-09 21:20:00            BAK        sb39
5    6 2006-04-09 11:40:00 2006-04-09 21:40:00            BAK        sb39
6    6 2006-04-09 12:00:00 2006-04-09 22:00:00            BAK        sb39
7    6 2006-04-09 12:20:00 2006-04-09 22:20:00            BAK        sb39
8    6 2006-04-09 12:40:00 2006-04-09 22:40:00            BAK        sb39
9    6 2006-04-09 13:00:00 2006-04-09 23:00:00            BAK        sb39
  sensor_depth_m temperature_c
1              2         29.63
2              2         29.56
3              2         29.51
4              2         29.53
5             10         29.57
6              2         29.60
7              2         29.66
8             10         29.68
9              2         29.68

# extracting all 10m depth sensors using subscripting
site          time_local            time_utc reef_type_code sensor_type
5    6 2006-04-09 11:40:00 2006-04-09 21:40:00            BAK        sb39
8    6 2006-04-09 12:40:00 2006-04-09 22:40:00            BAK        sb39
  sensor_depth_m temperature_c
5             10         29.57
8             10         29.68


Does that help?

Rgds,
Rainer
On Friday 28 October 2011 18:04:59 Vinny Moriarty wrote:
#
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