I'm trying to create a variable that illustrate change in dicthomous variable.
In other words, I have a dummy variable that illustrate whether something is active during a specific year. I need the new variable to account for change in the dummy variable while accounting for year and country. I have tried to illustrate the dummy I'm trying to create in the variable NewChangeVariable
below.
read.table(
text =
"Country, Year, Dummy, NewChangeVariable,
US, 1, 1, 0,
US, 2, 0, 0,
US, 3, 1, 1,
US, 4, 1, 0,
UK, 1, 0, 0,
UK, 2, 1, 1,
UK, 3, 1, 0,
UK, 4, 1, 0, ", sep = ",", header = TRUE)
The NewChangeVariable
is suppose to react to the first year a positive change in the dummy
takes places. Again, dependent on country.
Please dont hesitate to ask if anything is unclear
CodePudding user response:
Here is a tidyverse approach. First, you can group_by
Country
. Then, for each Country
, get the first row number where the prior Dummy
was 0, but that row's Dummy
is 1. Those rows meeting the criteria will be set to 1 (otherwise 0).
library(tidyverse)
df %>%
group_by(Country) %>%
mutate(NewChangeVariable2 = (row_number() == which(lag(Dummy) == 0 & Dummy == 1)[1]))
Output
Country Year Dummy NewChangeVariable NewChangeVariable2
<chr> <int> <int> <dbl> <int>
1 US 1 1 0 0
2 US 2 0 0 0
3 US 3 1 1 1
4 US 4 1 0 0
5 UK 1 0 0 0
6 UK 2 1 1 1
7 UK 3 1 0 0
8 UK 4 1 0 0