2021年3月30日星期二

In R: How to change the value of an observation conditionally?

If there is a value in the event date column for the first observation of a company (first row of companyID), I would like R to change the status to 0. Below you can see an example where there is a date in the first observation of companyID 1, so I would like the status to change to 0. However, for companyID 2, there is no date for its first observation so this first observation should remain a 1, and so on.

What I have:

companyID   status   event date     1          2      2012-10-13     1          2          NA     1          1      2014-01-06     1          1          NA     1          1          NA     2          1          NA     2          2      2005-05-22     2          2          NA     3          2      2008-03-11     3          2          NA     3          2          NA  

What I want:

companyID   status   event date     1          0      2012-10-13     1          2          NA     1          1      2014-01-06     1          1          NA     1          1          NA     2          1          NA     2          2      2005-05-22     2          2          NA     3          0      2008-03-11     3          2          NA     3          2          NA  

Let me know if it is unclear! Thank you in advance for any help :)

https://stackoverflow.com/questions/66879560/in-r-how-to-change-the-value-of-an-observation-conditionally March 31, 2021 at 07:03AM

没有评论:

发表评论