user12801590
user12801590

Reputation: 49

R: capture the value of the previous row

I have a code which identifies when property ownership has transitioned to a new owner. In the column, transition, the code allocates the value of "1" every time there is a transition and "0" if the property has not transitioned. The "1" is allocated to the new owner of the transition. This helps me capture data of all new buyers.

I need a code, now, which identifies the seller of the property (i.e. the row above the new owner with the value of 1). I believe this is possible with dplyr's lag function, but I am having trouble implementing it.

For example, if there is a transition between A (seller) and B (buyer), I can only currently identify the buyer B (transition=1) but want to identify the seller, too.

Here is the code for buyers:

transitions <- transitions %>%
  group_by(property) %>%
  mutate(transition = ifelse(name != dplyr::lag(name), 1, 0))

Upvotes: 0

Views: 73

Answers (2)

koenniem
koenniem

Reputation: 576

To me it looks like the code for a seller is similar to that of a buyer, only now we want to find the above where the name is different.

transitions %>% 
group_by(property) %>%  
mutate(seller = ifelse(lead(transition) == 1 & name!= lead(name), 1, 0))

Upvotes: 2

Johan Rosa
Johan Rosa

Reputation: 3152

For me this code worked, using dplyr::lag() as well. Note that the name of the seller is present in the first observation, when the transaction is done. If you want it in all the rows your can use fill

data %>%
  mutate(
    seller = ifelse(transition == 1, lag(name), NA)
  )

In the future try using a minimal reproducible example, you do not have to use the whole data frame, just a few rows that capture the problem you are facing.

Upvotes: 1

Related Questions