Hi everyone, I have a dataset that contains depths measurements every 30 sec for several days. 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! :) Thanks, Fabrice -- 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.
Rui Barradas
2013-Jan-12 14:56 UTC
[R] How to extract value for specific rows in an array?
Hello, If you want the maximum depth for each day, ?aggregate will do it. If you also want the date column, use ?merge after it. dat <- read.table(text = " 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 ", header = TRUE) agg <- aggregate(Depth ~ Day, data = dat, FUN = max) merge(agg, dat) Hope this helps, Rui Barradas Em 12-01-2013 05:32, fjaine escreveu:> Hi everyone, > > I have a dataset that contains depths measurements every 30 sec for several > days. > > 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! :) > Thanks, > Fabrice > > > > > -- > 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. > > ______________________________________________ > 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.
Reasonably Related Threads
- Code working but too slow, any idea for how to speed it up ?(no loop in it)
- Creating a bootable FS thats larger than 2880?
- isohybrid: slint64-14.1.iso: unable to find mac efi image
- problems with 2880 linux floppy image
- List of tables rather than an extra dimension in the table or (l)apply(xtabs)