gpt4 book ai didi

r - 根据前一组计算的另一个值传播值

转载 作者:行者123 更新时间:2023-12-01 10:23:21 25 4
gpt4 key购买 nike

使用此数据:

library(tidyverse)

df <-
structure(
list(
start_depth = c(10, 15, 20, 25, 30),
end_depth = c(15,
20, 25, 30, 35),
k = c(
0.136,
0.135,
0.133,
0.139,
0.132
)
),
row.names = c(NA,-5L),
class = c("tbl_df", "tbl", "data.frame"),
.Names = c("start_depth",
"end_depth", "k")
)

df
#> # A tibble: 5 x 3
#> start_depth end_depth k
#> <dbl> <dbl> <dbl>
#> 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_depthstart_depth 的值,增量为 1 米。

例如,假设我从 30-35 米级别的 start_val = 0.001 开始:

end_depth = 35

0.001000000 = 0.001000000 * exp(0.132 * (35 - (35)))

end_depth = 34

0.001141108 = 0.001000000 * exp(0.132 * (35 - (34)))

end_depth = 33

0.001302128 = 0.001000000 * exp(0.132 * (35 - (33)))

end_depth = 32

0.001485869 = 0.001000000 * exp(0.132 * (35 - (32)))

end_depth = 31

0.001695538 = 0.001000000 * exp(0.132 * (35 - (31)))

end_depth = 30

0.001934792 = 0.001000000 * exp(0.132 * (35 - (30)))

然后,25-30 米级别,我会重新开始,但使用上次计算的值(即 0.001934792)

end_depth = 30

0.001934792 * exp(0.139 * (30 - (30)))

end_depth = 29

0.001934792 * exp(0.139 * (30 - (29)))

我正在使用 dplyr,但任何其他选项都有效(例如:base R.data.table 等)

reprex package 创建于 2018-02-26 (v0.2.0).

最佳答案

这是一个带有 for 循环的选项

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]

}

-输出

lst
#[[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

如果我们使用tidyverse,那么可以使用pmapaccumulate_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

关于r - 根据前一组计算的另一个值传播值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48989773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com