[英]Using the result of a previous cell to compute the value in the current cell for the same variable in r
I am working with a customer database.我正在使用客户数据库。 I would like to compute the value of a cell in a column called EAD1 by using the computed value of previous cell for the same EAD1 column.我想通过使用同一 EAD1 列的前一个单元格的计算值来计算名为 EAD1 的列中的单元格的值。
An example below shows the data and expected result for EAD1 for data set called data1下面的示例显示了名为 data1 的数据集的 EAD1 的数据和预期结果
Acc_Num, Record_count, BRD, Account_Balance, EAD1
100, 1, 0.9, 100, 100
100, 2, 0.9, 100, 90
100, 3, 0.9, 100, 81
102, 1, 0.8, 100, 100
102, 2, 0.8, 100, 80
For Record_count = 1, EAD1 = Account_Balance
For Record_count > 1, EAD1 = EAD1 previous cell * BRD
Here's a solution using the dplyr
package.这是使用dplyr
package 的解决方案。 I've recreated your data using the tribble
function, but you won't need to worry about that, other than I've called the dataframe plain_data
.我已经使用tribble
function 重新创建了您的数据,但是您无需担心,除了我调用了 dataframe plain_data
。
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
plain_data <- tribble(~Acc_Num, ~Record_count, ~BRD, ~Account_Balance, ~EAD1,
100, 1, 0.9, 100, 100,
100, 2, 0.9, 100, 90,
100, 3, 0.9, 100, 81,
102, 1, 0.8, 100, 100,
102, 2, 0.8, 100, 80)
Here's the solution with dplyr
using the pipe operator.这是使用 pipe 运算符的dplyr
的解决方案。 We use mutate
to create a new column, called new_column
.我们使用mutate
创建一个新列,称为new_column
。 We use the if_else
to make the operation conditional on whether the value for each particular row in Record_count
is 1 or not.我们使用if_else
使操作以Record_count
中每个特定行的值是否为 1 为条件。 If the condition is met, new_column
takes the value of Account_Balance
, if not we use the lag
function on the column EAD1
, specifying the lag length to be 1 (denoting the previous row in data frame) and calculating that value with the current row's value of BRD
.如果满足条件,则new_column
采用Account_Balance
的值,否则我们在 EAD1 列上使用lag
EAD1
,将滞后长度指定为 1(表示数据帧中的前一行)并使用当前行的值计算该值BRD
的。
calculated_data <- plain_data %>%
mutate(new_column = if_else(
condition = Record_count == 1,
true = Account_Balance, # What gets calculated when Record_count is 1
false = lag(EAD1, 1) * BRD) # What gets calculated when Record_count is NOT 1
)
calculated_data
#> # A tibble: 5 × 6
#> Acc_Num Record_count BRD Account_Balance EAD1 new_column
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 100 1 0.9 100 100 100
#> 2 100 2 0.9 100 90 90
#> 3 100 3 0.9 100 81 81
#> 4 102 1 0.8 100 100 100
#> 5 102 2 0.8 100 80 80
Created on 2022-08-12 by the reprex package (v2.0.1)由代表 package (v2.0.1) 于 2022 年 8 月 12 日创建
Hope this helps.希望这可以帮助。 If not, let me know:)如果没有,请告诉我:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.