我有一个 Dataframe ,其中包含一个表示观察日期的变量(x1月0n1x),ID(Loan.Number
)和状态指示器(一至八)(Current.Delinquency.Code
)。我想计算一个新变量,该变量仅在任何ID的状态指示符第一次超过3时为真。在Excel中,我将写入=[Portfolio.Date]=minif([Portfolio.Date], [Loan.Number], [@Loan.Number], [Current.Delinquency.Code], ">3")
,但我不知道在R中怎么做。有人能帮我吗?
非常感谢!
我所寻找的是下面示例Data中[Delinquent
]列的公式,当"Loan.Number"的"Current.Delinquency.Code"观察值第一次大于3时,该公式跳转到"TRUE"。
Portfolio.Date Loan.Number Current.Delinquency.Code Delinquent
2022/01/01 1 1 FALSE
2022/02/01 1 4 TRUE
2022/03/01 1 4 FALSE
2022/04/01 1 4 FALSE
2022/01/01 2 1 FALSE
2022/02/01 2 1 FALSE
2022/03/01 2 1 FALSE
2022/04/01 2 1 FALSE
2022/01/01 3 1 FALSE
2022/02/01 3 3 FALSE
2022/03/01 3 4 TRUE
2022/04/01 3 4 FALSE
1条答案
按热度按时间toiithl61#
如果我理解正确话,这是一种可能的解决方案: