本文介绍了使用其他三个给定变量对两个同时变量进行迭代或逐行滚动计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
名为 crass
的数据集看起来像-
The dataset named crass
looks like -
> dput(crass)
structure(list(WT_TRADE_PRICE = c(3801, 3801, 3801, 3797, 3797,
3796.2125, 3800, 3797, 3795.09523809524, 3794, 3793, 3793, 3793.8,
3794.72, 3793.02777777778, 3789, 3790, 3788, 3788, 3788), min = c(3801,
3801, 3801, 3797, 3797, 3795, 3800, 3797, 3794, 3794, 3793, 3793,
3793, 3794, 3790, 3789, 3790, 3788, 3788, 3788), max = c(3801,
3801, 3801, 3797, 3797, 3800, 3800, 3797, 3797, 3794, 3793, 3793,
3794, 3797, 3794, 3789, 3790, 3788, 3788, 3788), Bid = c(3801,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA), Ask = c(3802, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, -20L
), class = c("tbl_df", "tbl", "data.frame"))
# A tibble: 20 x 5
WT_TRADE_PRICE min max Bid Ask
<dbl> <dbl> <dbl> <dbl> <dbl>
1 3801 3801 3801 3801 3802
2 3801 3801 3801 NA NA
3 3801 3801 3801 NA NA
4 3797 3797 3797 NA NA
5 3797 3797 3797 NA NA
6 3796. 3795 3800 NA NA
7 3800 3800 3800 NA NA
8 3797 3797 3797 NA NA
9 3795. 3794 3797 NA NA
10 3794 3794 3794 NA NA
11 3793 3793 3793 NA NA
12 3793 3793 3793 NA NA
13 3794. 3793 3794 NA NA
14 3795. 3794 3797 NA NA
15 3793. 3790 3794 NA NA
16 3789 3789 3789 NA NA
17 3790 3790 3790 NA NA
18 3788 3788 3788 NA NA
19 3788 3788 3788 NA NA
20 3788 3788 3788 NA NA
可以看出,两个变量 ask
& bid
仅具有需要使用以下逻辑迭代填充的初始值.
As can be seen, two variables ask
& bid
have only initial values which need to be filled iteratively using the following logic.
- 如果
WT_TRDAE_PRICE
是Ask
ORWT_TRADE_PRICE
的先前值> =
>将检查是否>
比先前的bid
和ask
的平均值-然后将当前的ask
设置为相等当前行的max
变量和bid
设置为等于先前的bid
值. - 否则,当前
ask
将设置为先前的ask
值&bid
到max
.
- If
WT_TRDAE_PRICE
is>=
than previous value ofAsk
ORWT_TRADE_PRICE
will be checked whether>
than mean of previousbid
andask
- then currentask
will be set equal to current rowmax
variable andbid
will be set equal to previousbid
value. - Else , current
ask
will be set to previousask
value &bid
tomax
.
伪代码-
if(WT_TRADE_PRICE >= L(Ask) | WT_TRADE_PRICE > (L(Bid)+L(Ask))/2)
{
Bid = L(Bid), Ask = max
}
else
{
Bid = min, Ask = L(Ask)
}
最终输出-
1 | 3801 | 3801 | 3801 | 3801 | 3802 |
2 | 3801 | 3801 | 3801 | 3801 | 3802 |
3 | 3801 | 3801 | 3801 | 3801 | 3802 |
4 | 3797 | 3797 | 3797 | 3797 | 3802 |
5 | 3797 | 3797 | 3797 | 3797 | 3802 |
6 | 3796. | 3795 | 3800 | 3795 | 3802 |
7 | 3800 | 3800 | 3800 | 3795 | 3800 |
8 | 3797 | 3797 | 3797 | 3797 | 3800 |
9 | 3795. | 3794 | 3797 | 3794 | 3800 |
10 | 3794 | 3794 | 3794 | 3794 | 3800 |
11 | 3793 | 3793 | 3793 | 3793 | 3800 |
12 | 3793 | 3793 | 3793 | 3793 | 3800 |
13 | 3794. | 3793 | 3794 | 3793 | 3800 |
14 | 3795. | 3794 | 3797 | 3793 | 3797 |
15 | 3793. | 3790 | 3794 | 3790 | 3797 |
16 | 3789 | 3789 | 3789 | 3789 | 3797 |
17 | 3790 | 3790 | 3790 | 3790 | 3797 |
18 | 3788 | 3788 | 3788 | 3788 | 3797 |
19 | 3788 | 3788 | 3788 | 3788 | 3797 |
20 | 3788 | 3788 | 3788 | 3788 | 3797 |
推荐答案
进一步编辑
根据请求,我尝试使用 purrr
样式的语法,该样式现在应该可以达到目的并且也应该很快
As per request I tries a purrr
style of syntax which should now serve the purpose and should be fast too
crass[1:3] %>%
nest_by(id = row_number()) %>%
ungroup() %>%
mutate(new = accumulate(data,
.init = list(Bid = 3801, Ask = 3802),
~ tibble(Bid = ifelse(.y$WT_TRADE_PRICE >= min(.x$Ask, (.x$Ask + .x$Bid)/2),
.x$Bid,
.y$min),
Ask = ifelse(.y$WT_TRADE_PRICE >= min(.x$Ask, (.x$Ask + .x$Bid)/2),
.y$max,
.x$Ask))
)[-1]) %>%
unnest_wider(data) %>%
unnest_wider(new)
# A tibble: 20 x 6
id WT_TRADE_PRICE min max Bid Ask
<int> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 3801 3801 3801 3801 3802
2 2 3801 3801 3801 3801 3802
3 3 3801 3801 3801 3801 3802
4 4 3797 3797 3797 3797 3802
5 5 3797 3797 3797 3797 3802
6 6 3796. 3795 3800 3795 3802
7 7 3800 3800 3800 3795 3800
8 8 3797 3797 3797 3797 3800
9 9 3795. 3794 3797 3794 3800
10 10 3794 3794 3794 3794 3800
11 11 3793 3793 3793 3793 3800
12 12 3793 3793 3793 3793 3800
13 13 3794. 3793 3794 3793 3800
14 14 3795. 3794 3797 3794 3800
15 15 3793. 3790 3794 3790 3800
16 16 3789 3789 3789 3789 3800
17 17 3790 3790 3790 3790 3800
18 18 3788 3788 3788 3788 3800
19 19 3788 3788 3788 3788 3800
20 20 3788 3788 3788 3788 3800
为循环修改的更早版本
for(i in 2:nrow(crass)){
if(crass[i, 1] >= min(crass[i-1, 5], (crass[i-1, 4] + crass[i-1, 5])/2)){
crass[i, 5] <- crass[i, 3]
crass[i, 4] <- crass[i-1, 4]
} else {
crass[i, 4] <- crass[i, 2]
crass[i, 5] <- crass[i-1, 5]
}
}
crass
# A tibble: 20 x 5
WT_TRADE_PRICE min max Bid Ask
<dbl> <dbl> <dbl> <dbl> <dbl>
1 3801 3801 3801 3801 3802
2 3801 3801 3801 3801 3802
3 3801 3801 3801 3801 3802
4 3797 3797 3797 3797 3802
5 3797 3797 3797 3797 3802
6 3796. 3795 3800 3795 3802
7 3800 3800 3800 3795 3800
8 3797 3797 3797 3797 3800
9 3795. 3794 3797 3794 3800
10 3794 3794 3794 3794 3800
11 3793 3793 3793 3793 3800
12 3793 3793 3793 3793 3800
13 3794. 3793 3794 3793 3800
14 3795. 3794 3797 3794 3800
15 3793. 3790 3794 3790 3800
16 3789 3789 3789 3789 3800
17 3790 3790 3790 3790 3800
18 3788 3788 3788 3788 3800
19 3788 3788 3788 3788 3800
20 3788 3788 3788 3788 3800
crass
在运行for循环之前
crass
before running of for loop
# A tibble: 20 x 5
WT_TRADE_PRICE min max Bid Ask
<dbl> <dbl> <dbl> <dbl> <dbl>
1 3801 3801 3801 3801 3802
2 3801 3801 3801 NA NA
3 3801 3801 3801 NA NA
4 3797 3797 3797 NA NA
5 3797 3797 3797 NA NA
6 3796. 3795 3800 NA NA
7 3800 3800 3800 NA NA
8 3797 3797 3797 NA NA
9 3795. 3794 3797 NA NA
10 3794 3794 3794 NA NA
11 3793 3793 3793 NA NA
12 3793 3793 3793 NA NA
13 3794. 3793 3794 NA NA
14 3795. 3794 3797 NA NA
15 3793. 3790 3794 NA NA
16 3789 3789 3789 NA NA
17 3790 3790 3790 NA NA
18 3788 3788 3788 NA NA
19 3788 3788 3788 NA NA
20 3788 3788 3788 NA NA
这篇关于使用其他三个给定变量对两个同时变量进行迭代或逐行滚动计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!