Unbalaced panel data - dummy variable

Hello,

I have an unbalanced panel data set named "df" (n individuals and 8 years). I want to create a dummy variable that takes 1 if the value of variable 1 for each "Name" does not change for all years and 0 otherwise.
For example:

years Names Variable1 Dummy variable
2008 A 1 1
2009 A 1 1
2011 A 1 1
2012 A 1 1
2013 A 1 1
2014 A 1 1
2015 A 1 1
2008 B 0 0
2009 B 1 1
2010 B 0 1
2012 B 1 1
2015 B 1 1

Could you please help me?
Thanks

This is what I understand from your description

library(dplyr)

df <- data.frame(
  stringsAsFactors = FALSE,
             years = c(2008,2009,2011,2012,2013,
                       2014,2015,2008,2009,2010,2012,2015),
             Names = c("A","A","A","A","A","A",
                       "A","B","B","B","B","B"),
         Variable1 = c(1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1),
    Dummy_variable = c(1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1)
)

df %>% 
    group_by(Names) %>% 
    mutate(Dummy_variable = as.numeric(length(unique(Variable1)) == 1))
#> # A tibble: 12 x 4
#> # Groups:   Names [2]
#>    years Names Variable1 Dummy_variable
#>    <dbl> <chr>     <dbl>          <dbl>
#>  1  2008 A             1              1
#>  2  2009 A             1              1
#>  3  2011 A             1              1
#>  4  2012 A             1              1
#>  5  2013 A             1              1
#>  6  2014 A             1              1
#>  7  2015 A             1              1
#>  8  2008 B             0              0
#>  9  2009 B             1              0
#> 10  2010 B             0              0
#> 11  2012 B             1              0
#> 12  2015 B             1              0

Created on 2021-03-28 by the reprex package (v1.0.0.9002)

1 Like

Thanks a lot! It works!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.