简体   繁体   English

用 dplyr 跨/行替换 for 循环?

[英]replace for loop with dplyr across / rowwise?

I'm having a hard time refactoring a for loop into a dplyr pipe. I need to reference the dataframa a and the previously calculated row.我很难将 for 循环重构为 dplyr pipe。我需要引用数据框 a 和之前计算的行。 Any advice how to get b from a on a dplyr pipe?关于如何从 dplyr pipe 上的 a 获取 b 的任何建议?

Many thanks!非常感谢!

a <- tibble::tribble(~ 'a',  ~ 'b',  ~ 'c',
                     .1, .2, .3,
                     .2, .4, .6,
                     .3, .6, .9)
b <- a

for (i in 2:nrow(a)) {
  b[i, ] <- b[i - 1, ] + b[i, ] * (1 - b[i - 1, ])
}


c <- a |>
  dplyr::mutate(dplyr::across(where(is.numeric),
                              ~ dplyr::lag(.x, 1, 0) +
                                .x *
                                (1 - dplyr::lag(.x, 1, 0))))

d <- a |> dplyr::rowwise( )|>
  dplyr::mutate(dplyr::across(where(is.numeric),
                              ~ dplyr::lag(.x, 1, 0) +
                                .x *
                                (1 - dplyr::lag(.x, 1, 0))))
identical(b,c)
identical(b,d)


You can use Reduce() (or purrr::accumulate() if you prefer).您可以使用Reduce() (如果您愿意,也可以使用purrr::accumulate() )。

library(dplyr)

a |>
  mutate(across(where(is.numeric), \(v) Reduce(\(x, y) x + y * (1 - x) , v, accumulate = TRUE)))

# A tibble: 3 × 3
      a     b     c
  <dbl> <dbl> <dbl>
1 0.1   0.2   0.3  
2 0.28  0.52  0.72 
3 0.496 0.808 0.972

I prefer the Reduce() way.我更喜欢Reduce()方式。 Here is an attempt to incorporate a loop into mutate() .这是将循环合并到mutate()中的尝试。

a %>%
  mutate(across(, ~ {
    for(i in 2:length(.x)) {
      .x[i] <- .x[i - 1] + .x[i] * (1 - .x[i - 1])
    }; .x
  }))

# # A tibble: 3 × 3
#       a     b     c
#   <dbl> <dbl> <dbl>
# 1 0.1   0.2   0.3  
# 2 0.28  0.52  0.72 
# 3 0.496 0.808 0.972

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM