簡體   English   中英

在 mutate() 函數中使用 lag() 的問題 (tidyverse)

[英]Problem using lag() within a mutate() function (tidyverse)

我正在嘗試將另一列添加到數據框中,其中新列是新列中前一個值和當前行值的函數。 我試圖去除不相關的代碼並堅持使用簡單的數字,以便我可以理解這里的答案。 給定以下數據框:

  x
1 1
2 2
3 3
4 4
5 5

下一列 (y) 將 5 添加到 x 並添加前一行的 y 值。 第一行中沒有 y 的先前值,因此我將其定義為 0。因此 y 的第一行值將是 x+5+0 或 1+5+0 或 6。第二行將是 x+5+ y(from 1st row) or 2+5+6 or 13. 數據框應該是這樣的:

  x  y
1 1  6
2 2 13
3 3 21
4 4 30
5 5 40

我用 case_when() 和 lag() 函數試過這個:

test_df <- data.frame(x = 1:5)
test_df %>% mutate(y = case_when(x==1 ~ 6,
+                                    x>1 ~ x+5+lag(y)))

錯誤: mutate()y y = case_when(x == 1 ~ 6, x > 1 ~ x + 5 + lag(y)) 未找到 x 對象 'y' 運行rlang::last_error()以查看錯誤發生的位置。

我以為 y 是在計算第一行時定義的。 有一個更好的方法嗎? 謝謝!

你根本不需要lag 只需一個cumsum就足夠了。

test_df %>% mutate(y = cumsum(x + 5))

#>   x  y
#> 1 1  6
#> 2 2 13
#> 3 3 21
#> 4 4 30
#> 5 5 40

數據

test_df <- data.frame(x = 1:5)

我們也可以在這里使用purrr::accumulate

library(purrr)

df %>% mutate(y = accumulate(x+5, ~.x + .y))

  x  y
1 1  6
2 2 13
3 3 21
4 4 30
5 5 40

我們還可以將accumulate與常規基礎 R 合成accumulate一起使用:

df %>% mutate(y = accumulate(x+5, function(x, y) {x + y}))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM