I have a dataframe with a list of (space-separated) years that I would like to turn into dummies for each year.
Consider the following toy data:
raw <- data.frame(textcol = c("case1", "case2", "case3"), years=c('1996 1997 1998','1997 1999 2000', '1996 1998 2000'))
textcol years
1 case1 1996 1997 1998
2 case2 1997 1999 2000
3 case3 1996 1998 2000
I would now like to transform the data frame into this
textcol `1996` `1997` `1998` `1999` `2000`
1 case1 1 1 1 0 0
2 case2 0 1 0 1 1
3 case3 1 0 1 0 1
I tried using separate()
and str_split()
to no avail. Can someone point me to the right approach?