I have the following code. Please see the explanation in ###. Data is two columns with Time and velocity at 10Hz sampling rate
Time | velocity |
---|---|
0.0000000 | 0.3444447 |
0.0099998 | 0.3444447 |
0.0200003 | 0.3444447 |
0.0300001 | 0.3444447 |
0.0399999 | 0.3444447 |
0.0499997 | 0.3444447 |
0.0600002 | 0.3444447 |
0.0700000 | 0.3444447 |
0.0799998 | 0.3444447 |
0.0900003 | 0.3444447 |
0.1000001 | 0.3444447 |
0.1099999 | 0.3444447 |
0.1199997 | 0.3444447 |
0.1300002 | 0.3444447 |
0.1400000 | 0.3444447 |
0.1499998 | 0.3444447 |
0.1600003 | 0.3444447 |
0.1700001 | 0.3444447 |
0.1799999 | 0.3444447 |
0.1899997 | 0.4416670 |
0.2000002 | 0.4416670 |
0.2100000 | 0.4416670 |
0.2199998 | 0.4416670 |
0.2300003 | 0.4416670 |
0.2400001 | 0.6222227 |
0.2499999 | 0.6222227 |
0.2599997 | 0.6222227 |
... and so on
#create interval
from=min(data$Time)
to=max(data$Time)
breaks=seq(from, to, by=0.2)
#function to select values in each interval and create a list, I am trying to find the highest velocity value in each interval of time BUT only IF it is greater than the value in the previous time interval
interval <- lapply(1:(length(breaks)-1L), function(i) {
x <- subset(sprint1, Time >= breaks[i] & Time <= breaks[i 1L])
x[x$velocity == max(x$velocity[1]), ]
})
#function to round values to two decimal places
r <- function(x) format(round(x, 2), nsmall = 2L)
#assign names to each element of interval
for(i in seq_along(interval)) {
names(interval)[i] <- paste0(r(breaks[i]), '-', r(breaks[i 1L]))
}
interval ##output
You can create dataset with Time and velocity.
CodePudding user response:
I don't think you need a list or loop. Here's my suggestion, based on my understanding of your issue:
First, define your intervals based on breaks using cut()
:
library(dplyr)
# Define breaks for intervals ("groups")
breaks <- seq(0, 0.28, by = 0.02)
# Group data using breaks
dat$interval <- cut(dat$Time, breaks = breaks, right = FALSE, labels = FALSE)
Now use dplyr to manipulate your data based on your condition: "find the highest velocity value in each interval of time BUT only IF it is greater than the value in the previous time interval."
dat2 <- dat %>%
# For each group, as defined by an interval
group_by(interval) %>%
# Get the maximum velocity value
mutate(interval_max = max(velocity)) %>%
# Now ungroup and iterate across rows
ungroup %>%
# If the previous value is greater than the current value,
# keep the previous value, if not, keep the current value
mutate(interval_max_cond = ifelse(
lag(interval_max) > interval_max,
lag(interval_max),
interval_max))
Let me know if you have any questions!
data:
dat <- structure(list(Time = c(0, 0.0099998, 0.0200003, 0.0300001, 0.0399999,
0.0499997, 0.0600002, 0.07, 0.0799998, 0.0900003, 0.1000001,
0.1099999, 0.1199997, 0.1300002, 0.14, 0.1499998, 0.1600003,
0.1700001, 0.1799999, 0.1899997, 0.2000002, 0.21, 0.2199998,
0.2300003, 0.2400001, 0.2499999, 0.2599997), velocity = c(0.3444447,
0.3444447, 0.3444447, 0.3444447, 0.3444447, 0.3444447, 0.3444447,
0.3444447, 0.3444447, 0.3444447, 0.3444447, 0.3444447, 0.3444447,
0.3444447, 0.3444447, 0.3444447, 0.3444447, 0.3444447, 0.3444447,
0.441667, 0.441667, 0.441667, 0.441667, 0.441667, 0.6222227,
0.6222227, 0.6222227)), class = "data.frame", row.names = c(NA,
-27L))