[R] How to extract value for specific rows in an array?

fjaine f.jaine at uq.edu.au
Sat Jan 12 06:32:14 CET 2013

Hi everyone,

I have a dataset that contains depths measurements every 30 sec for several

The subset of data that I am working on looks like this:

Date            Day           Depth
18442.00    29              41.0
18442.00    29              43.5
18442.04    29              40.3
18442.04    29              35.1
18443.00    30              16.5

Since my data is collected every 30 sec, I want to find the maximum depth
recorded for each day and recorded daily maximum depths into a new array.
Now, I would like to also sample the date for each maximum depth value. 

So far I have been able to sample the max daily depth in a 1-column array
using the loop below. I tried creating a new empty array for recording the
date for each daily max depth value, but it doesn't seem to be working...
Any ideas or suggestions?

DF <- data.frame(date,day,depth)
samplesperday <- 2*60*24  # number of datapoints per day (2880 here)
nbdays<- nrow(DF)/samplesperday  # number of days in the dataset (5 here)
MaxDepth <- array(NaN,c(nbdays,1))  # create empty array where max depth
values will be stored
Date <- array(NaN,c(nbdays,1))  # create empty array where date for max
depth values will be stored

for (i in 1:nbdays){
  dayIndex1 <- samplesperday*(i-1)+1 # start of day period (value 1 of 2880)
  dayIndex2 <- samplesperday*(i) # end of day period (value 2880 of 2880)    
  MaxDepth[i] <- max(DF$depth[dayIndex1:dayIndex2])  # extract max depth
value from the 2880 datapoints collected per day
  Date [i] <- DF$date[MaxDepth[i]]  ### NOT WORKING...

Another way to do this, since I am getting one daily max depth value, would
be to sample the date every day. Since I have 2880 measurements per day, I
would just have to sample the date every 2881 rows. This said, I am stuck
and can't figure out out to do this... 

Your help would be most appreciated! :)

View this message in context: http://r.789695.n4.nabble.com/How-to-extract-value-for-specific-rows-in-an-array-tp4655323.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list