[R] Extracting data by row

Rainer Schuermann rainer.schuermann at gmx.net
Sat Oct 29 08:56:30 CEST 2011


On Friday 28 October 2011 18:04:59 Vinny Moriarty wrote:
> Thanks everyone for you help with my last question, and now I have one
> last one...

Here is what I would do, based on my understanding of your question:

# your data snippet as data frame
> x
  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
> Ten <- x[x["sensor_depth_m"]==10,]
> Ten
  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:
> Thanks everyone for you help with my last question, and now I have one
> last one...
> 
> 
> Here is a sample of my data in .csv format
> 
> site,time_local,time_utc,reef_type_code,sensor_type,sensor_depth_m,temper
> ature_c 06,2006-04-09 10:20:00,2006-04-09 20:20:00,BAK,sb39, 2, 29.63
> 06,2006-04-09 10:40:00,2006-04-09 20:40:00,BAK,sb39, 2, 29.56
> 06,2006-04-09 11:00:00,2006-04-09 21:00:00,BAK,sb39, 2, 29.51
> 06,2006-04-09 11:20:00,2006-04-09 21:20:00,BAK,sb39, 2, 29.53
> 06,2006-04-09 11:40:00,2006-04-09 21:40:00,BAK,sb39, 10, 29.57
> 06,2006-04-09 12:00:00,2006-04-09 22:00:00,BAK,sb39, 2, 29.60
> 06,2006-04-09 12:20:00,2006-04-09 22:20:00,BAK,sb39, 2, 29.66
> 06,2006-04-09 12:40:00,2006-04-09 22:40:00,BAK,sb39, 10, 29.68
> 06,2006-04-09 13:00:00,2006-04-09 23:00:00,BAK,sb39, 2, 29.68
> 
> 
> My goal was to extract all of the rows from a certain depth. Using the
> column "sensor_depth_m" to order my data by, I wanted all of the data
> from 10m. So this is what I wanted when I finished
> 
> site,time_local,time_utc,reef_type_code,sensor_type,sensor_depth_m,temper
> ature_c 06,2006-04-09 11:40:00,2006-04-09 21:40:00,BAK,sb39, 10, 29.57
> 06,2006-04-09 12:40:00,2006-04-09 22:40:00,BAK,sb39, 10, 29.68
> 06,2006-04-09 13:00:00,2006-04-09 23:00:00,BAK,sb39, 10, 29.68
> 
> 
> 
> 
> To pull out all of the data from a 10m sensor depth I came up with the
> code:
> 
> Ten<-dataTable1[(dataTable1$sensor_depth_m=="10"),]
> 
> 
> But when I run it I just get an extra column tacked onto the end like
> this
> 
> 
> site,time_local,time_utc,reef_type_code,sensor_type,sensor_depth_m,temper
> ature_c, sensor_depth_m
> 06,2006-04-09 10:20:00,2006-04-09 20:20:00,BAK,sb39, 2, 29.63,10
> 06,2006-04-09 10:40:00,2006-04-09 20:40:00,BAK,sb39, 2, 29.56,10
> 06,2006-04-09 11:00:00,2006-04-09 21:00:00,BAK,sb39, 2, 29.51,10
> 06,2006-04-09 11:20:00,2006-04-09 21:20:00,BAK,sb39, 2, 29.53,10
> 06,2006-04-09 11:40:00,2006-04-09 21:40:00,BAK,sb39, 10, 29.57,10
> 06,2006-04-09 12:00:00,2006-04-09 22:00:00,BAK,sb39, 2, 29.60,10
> 06,2006-04-09 12:20:00,2006-04-09 22:20:00,BAK,sb39, 2, 29.66,10
> 06,2006-04-09 12:40:00,2006-04-09 22:40:00,BAK,sb39, 10, 29.68,10
> 06,2006-04-09 13:00:00,2006-04-09 23:00:00,BAK,sb39, 10, 29.68,10
> 
> 
> It seems pretty straight forward, I'm not sure what I am missing.
> 
> 
> Thanks
> 
> V
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html and provide commented,
> minimal, self-contained, reproducible code.



More information about the R-help mailing list