我基本上在 data.frame 中有兩列(向量),速度和加速度如下:
speed acceleration
1 3.2694444 2.6539535522
2 3.3388889 2.5096979141
3 3.3888889 2.2722134590
4 3.4388889 1.9815256596
5 3.5000000 1.6777544022
6 3.5555556 1.3933215141
7 3.6055556 1.1439051628
8 3.6527778 0.9334115982
9 3.6722222 0.7561602592
我需要找到 x 軸上的每個值速度(速度),y 軸(加速度)的前 10% 最大值是多少。這也需要在特定的時間間隔內。例如速度 3.2-3.4、3.4-3.6 等。你能告訴我在這種情況下 for 回圈會是什么樣子嗎?
uj5u.com熱心網友回復:
正如@alistaire 已經指出的那樣,您提供的資料量非常有限。所以我們首先必須模擬更多的資料,基于這些資料我們可以測驗我們的代碼。
set.seed(1)
# your data
speed <- c(3.2694444, 3.3388889, 3.3388889, 3.4388889, 3.5,
3.5555556, 3.6055556, 3.6527778, 3.6722222)
acceleration <- c(2.6539535522, 2.5096979141, 2.2722134590,
1.9815256596, 1.6777544022, 1.3933215141,
1.1439051628, 0.9334115982, 0.7561602592)
df <- data.frame(speed, acceleration)
# expand data.frame and add a little bit of noise to all values
# to make them 'unique'
df <- as.data.frame(do.call(
rbind,
replicate(15L, apply(df, 2, \(x) (x runif(length(x), -1e-1, 1e-1) )),
simplify = FALSE)
))
create_intervals顧名思義,該函式創建用戶定義的間隔。其余代碼執行“繁重的作業”并將所需的結果存盤在out.
如果你想有間隔speed相等的寬度,只需指定團體(的數量n_groups(即)你想有離開的剩余引數lwr,upr和interval_span)指定。
# Cut speed into user-defined intervals
create_intervals <- \(n_groups = NULL, lwr = NULL, upr = NULL, interval_span = NULL) {
if (!is.null(lwr) & !is.null(upr) & !is.null(interval_span) & is.null(n_groups)) {
speed_low <- subset(df, speed < lwr, select = speed)
first_interval <- with(speed_low, c(min(speed), lwr))
middle_intervals <- seq(lwr interval_span, upr - interval_span, interval_span)
speed_upp <- subset(df, speed > upr, select = speed)
last_interval <- with(speed_upp, c(upr, max(speed)))
intervals <- c(first_interval, middle_intervals, last_interval)
} else {
step <- with(df, c(max(speed) - min(speed))/n_groups)
intervals <- array(0L, dim = n_groups)
for(i in seq_len(n_groups)) {
intervals[i] <- min(df$speed) i * step
}
}
return(intervals)
}
# three intervals with equal width
my_intervals <- create_intervals(n_groups = 3L)
# Compute values of speed when acceleration is greater then
# or equal to the 90th percentile
out <- lapply(1:(length(my_intervals)-1L), \(i) {
x <- subset(df, speed >= my_intervals[i] & speed <= my_intervals[i 1L])
x[x$acceleration >= quantile(x$acceleration, 0.9), ]
})
# function to round values to two decimal places
r <- \(x) format(round(x, 2), nsmall = 2L)
# assign names to each element of out
for(i in seq_along(out)) {
names(out)[i] <- paste0(r(my_intervals[i]), '-', r(my_intervals[i 1L]))
}
輸出 1
> out
$`3.38-3.57`
speed acceleration
11 3.394378 2.583636
21 3.383631 2.267659
57 3.434123 2.300234
83 3.394886 2.580924
101 3.395459 2.460971
$`3.57-3.76`
speed acceleration
6 3.635234 1.447290
41 3.572868 1.618293
51 3.615017 1.420020
95 3.575412 1.763215
我們還可以speed根據比等間隔的速度間隔更“有意義”的間隔來計算所需的值,例如 [ min(speed), 3.3)、[3.3, 3.45)、[3.45, 3.6) 和 [3.6, max(speed))。
這可以通過留下來完成n_groups不明確的,而不是指定lwr,upr和interval_span有意義。例如,當下限為 3.3 且上限為 3.6 時,區間跨度為 0.15 是有意義的。
# custom boundaries based on a lower limit and upper limit
my_intervals <- create_intervals(lwr = 3.3, upr = 3.6, interval_span = 0.15)
輸出 2
> out
$`3.18-3.30`
speed acceleration
37 3.238781 2.696456
82 3.258691 2.722076
$`3.30-3.45`
speed acceleration
11 3.394378 2.583636
19 3.328292 2.711825
73 3.315306 2.644580
83 3.394886 2.580924
$`3.45-3.60`
speed acceleration
4 3.520530 2.018930
40 3.517329 2.032943
58 3.485247 2.079893
67 3.458031 2.078545
$`3.60-3.76`
speed acceleration
6 3.635234 1.447290
34 3.688131 1.218969
51 3.615017 1.420020
78 3.628465 1.348873
注意:如果您使用 R <4.1.0 的版本,請使用function(x)而不是\(x)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/408642.html
標籤:
上一篇:在一個范圍內迭代超過生成所需輸出所需的次數是否有任何不利之處?
下一篇:使用嵌套回圈將資料附加到CSV
