R 基于上一组中计算的另一个值传播值
使用这些数据:R 基于上一组中计算的另一个值传播值,r,dplyr,R,Dplyr,使用这些数据: 库(tidyverse) df#A tible:5 x 3 #>开始深度结束深度k #> #> 1 10.0 15.0 0.136 #> 2 15.0 20.0 0.135 #> 3 20.0 25.0 0.133 #> 4 25.0 30.0 0.139 #> 5 30.0 35.0 0.132 我想使用以下等式传播每对end\
库(tidyverse)
df#A tible:5 x 3
#>开始深度结束深度k
#>
#> 1 10.0 15.0 0.136
#> 2 15.0 20.0 0.135
#> 3 20.0 25.0 0.133
#> 4 25.0 30.0 0.139
#> 5 30.0 35.0 0.132
我想使用以下等式传播每对end\u depth
和start\u depth
的值,增量为1米
例如,假设我从30-35米级的start\u val=0.001开始:
在end\u深度=35时
0.001000000=0.001000000*exp(0.132*(35-(35)))
在end\u深度=34时
0.001141108=0.001000000*exp(0.132*(35-(34)))
在end\u深度=33时
0.001302128=0.001000000*exp(0.132*(35-(33)))
在end\u深度=32时
0.001485869=0.001000000*exp(0.132*(35-(32)))
在end\u深度=31时
0.001695538=0.001000000*exp(0.132*(35-(31)))
在end\u深度=30时
0.001934792=0.001000000*exp(0.132*(35-(30)))
然后,25-30米级,我将再次开始,但使用最后计算的值(即0.001934792)
在end\u深度=30时
0.001934792*经验值(0.139*(30-(30)))
在end\u深度=29时
0.001934792*经验值(0.139*(30-(29)))
我使用的是dplyr,但任何其他选项都是有效的(例如:base R.data.table等)
由(v0.2.0)于2018年2月26日创建。使用for
循环的硬编码解决方案
# First adding a vector with starting values
df1 <- df
df1$start_val <- c(rep(NA, 4),0.001)
# the loop
res <- list()
for (i in nrow(df1):1){
# for which values to calculated by increment 1
index <- df1$end_depth[i]:df1$start_depth[i]
tmp <- sapply(index, function(x){
df1$start_val[i] * exp(df$k[i] * (max(index) - (x)))
})
df_tmp <- cbind(index, tmp)
df1$start_val[i-1] <- df_tmp[nrow(df_tmp),2]
res[[i]] <- df_tmp
}
df1
# A tibble: 5 x 4
start_depth end_depth k start_val
<dbl> <dbl> <dbl> <dbl>
1 10 15 0.136 0.014805519
2 15 20 0.135 0.007538325
3 20 25 0.133 0.003876761
4 25 30 0.139 0.001934792
5 30 35 0.132 0.001000000
lapply(res, tail, 2)
[[1]]
index tmp
[5,] 11 0.02550820
[6,] 10 0.02922428
[[2]]
index tmp
[5,] 16 0.01293582
[6,] 15 0.01480552
[[3]]
index tmp
[5,] 21 0.006599540
[6,] 20 0.007538325
[[4]]
index tmp
[5,] 26 0.003373666
[6,] 25 0.003876761
[[5]]
index tmp
[5,] 31 0.001695538
[6,] 30 0.001934792
v <- 0.001000000
lst <- vector("list", nrow(df))
for(i in rev(seq_along(lst))) {
e1 <- v * exp(df$k[i] *(df$end_depth[i] -
seq(df$start_depth[i], df$end_depth[i], by = 1)))
lst[[i]] <- e1
v <- e1[1]
}
#首先添加带有起始值的向量
df1这里有一个带有的选项,用于循环
# First adding a vector with starting values
df1 <- df
df1$start_val <- c(rep(NA, 4),0.001)
# the loop
res <- list()
for (i in nrow(df1):1){
# for which values to calculated by increment 1
index <- df1$end_depth[i]:df1$start_depth[i]
tmp <- sapply(index, function(x){
df1$start_val[i] * exp(df$k[i] * (max(index) - (x)))
})
df_tmp <- cbind(index, tmp)
df1$start_val[i-1] <- df_tmp[nrow(df_tmp),2]
res[[i]] <- df_tmp
}
df1
# A tibble: 5 x 4
start_depth end_depth k start_val
<dbl> <dbl> <dbl> <dbl>
1 10 15 0.136 0.014805519
2 15 20 0.135 0.007538325
3 20 25 0.133 0.003876761
4 25 30 0.139 0.001934792
5 30 35 0.132 0.001000000
lapply(res, tail, 2)
[[1]]
index tmp
[5,] 11 0.02550820
[6,] 10 0.02922428
[[2]]
index tmp
[5,] 16 0.01293582
[6,] 15 0.01480552
[[3]]
index tmp
[5,] 21 0.006599540
[6,] 20 0.007538325
[[4]]
index tmp
[5,] 26 0.003373666
[6,] 25 0.003876761
[[5]]
index tmp
[5,] 31 0.001695538
[6,] 30 0.001934792
v <- 0.001000000
lst <- vector("list", nrow(df))
for(i in rev(seq_along(lst))) {
e1 <- v * exp(df$k[i] *(df$end_depth[i] -
seq(df$start_depth[i], df$end_depth[i], by = 1)))
lst[[i]] <- e1
v <- e1[1]
}
如果我们使用的是tidyverse
,则可以使用pmap
和acculate\u right
library(purrr)
pmap(df, ~ exp(..3 *(..2 - seq(..1, ..2, by = 1)))) %>%
accumulate_right(~ .x[1] * .y, .init = 0.001000000) %>%
head(., -1)
#[[1]]
#[1] 0.02922428 0.02550820 0.02226465 0.01943353 0.01696241 0.01480552
#[[2]]
#[1] 0.014805519 0.012935817 0.011302229 0.009874938 0.008627890 0.007538325
#[[3]]
#[1] 0.007538325 0.006599540 0.005777667 0.005058146 0.004428230 0.003876761
#[[4]]
#[1] 0.003876761 0.003373666 0.002935859 0.002554867 0.002223316 0.001934792
#[[5]]
#[1] 0.001934792 0.001695538 0.001485869 0.001302128 0.001141108 0.001000000
一切正常。谢谢。@Philippemasicotte谢谢,我用accumulate