如何 select R 列表中特定列的第一行
How to select the first row of a specific column in a list in R
我有以下代码。请看###中的解释。数据是两列,时间和速度为 10Hz 采样率
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
...等等
#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
您可以使用时间和速度创建数据集。
我认为您不需要列表或循环。根据我对您的问题的理解,这是我的建议:
首先,使用 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)
现在使用 dplyr 根据您的条件操作您的数据:“找到每个时间间隔中的最高速度值,但前提是它大于前一个时间间隔中的值。”
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))
如果您有任何问题,请告诉我!
数据:
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))
我有以下代码。请看###中的解释。数据是两列,时间和速度为 10Hz 采样率
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 |
...等等
#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
您可以使用时间和速度创建数据集。
我认为您不需要列表或循环。根据我对您的问题的理解,这是我的建议:
首先,使用 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)
现在使用 dplyr 根据您的条件操作您的数据:“找到每个时间间隔中的最高速度值,但前提是它大于前一个时间间隔中的值。”
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))
如果您有任何问题,请告诉我!
数据:
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))