Last active
November 24, 2020 19:32
-
-
Save moritzpschwarz/94a532dd6bc9345bb0525fce9b0d79f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(plm) | |
library(tidyverse) | |
library(fastDummies) | |
data(EmplUK) | |
EmplUK %>% | |
select(-sector) %>% | |
dummy_cols(.data = .,select_columns = c("firm","year"),remove_selected_columns = TRUE,remove_first_dummy = TRUE) -> paneldata | |
head(paneldata) | |
# This just defines a function that allows you to recreate the original dataset | |
# By this, I mean that it gives you back the "country" and "id" columns from the dummy variables of the isat object | |
getfactorback <- function(data, | |
groupdummyprefix, | |
timedummyprefix, | |
grouplabel, | |
timelabel, | |
firstgroup, | |
firsttime) { | |
data %>% | |
mutate(newgroup = ifelse(rowSums(cur_data() %>% select(starts_with(groupdummyprefix)))==1,0,1), | |
newtime = ifelse(rowSums(cur_data() %>% select(starts_with(timedummyprefix)))==1,0,1)) %>% | |
rename(!!paste0(groupdummyprefix,firstgroup):=newgroup, | |
!!paste0(timedummyprefix,firsttime):=newtime) %>% | |
pivot_longer(cols = starts_with(groupdummyprefix),names_to = grouplabel,names_prefix = groupdummyprefix) %>% | |
filter(value == 1) %>% | |
select(-value) %>% | |
pivot_longer(cols = starts_with(timedummyprefix),names_to = timelabel,names_prefix = timedummyprefix) %>% | |
filter(value == 1) %>% | |
select(-value) %>% | |
mutate(across(.cols = c(all_of(grouplabel),all_of(timelabel)),factor)) %>% | |
relocate(all_of(c(grouplabel,timelabel))) -> output | |
return(output) | |
} | |
getfactorback(data = paneldata, | |
groupdummyprefix = "firm_", | |
grouplabel = "firm", | |
timedummyprefix = "year_", | |
timelabel = "year", | |
firstgroup = "1", | |
firsttime = 1976) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment