For each variable in var1, I want its interaction with each variable in var2. In Stata, I can simply use a nested foreach loop to do this but I am not able to replicate the logic in R.
Stata code:
foreach var1 in
gdp_g gdp_g_l GPCP_g GPCP_g_l
{;
foreach var2 in
polity2l y_0 ethfrac Oil lmtnest
{;
quietly gen `var1'_`var2' = `var1'*`var2';
};
};
Not sure about the intuition in R.
vars1 <- list("gdp_g", "gdp_g_l", "GPCP_g", "GPCP_g_l")
vars2 <- list("polity2l", "y_0", "ethfrac", "Oil", "lmtnest")
multiplyit <- function(x){
paste(x, collapse = "*")
}
for(i in 1:length(vars1)) {
for(j in 1:length(var2)){
vars1[i]*vars2[j]
}
}
Maybe I need to use a formula to multiply each unique combination of variables.