[R] Dummy (factor) based on a pair of variables

Jason Morgan jwm-r-help at skepsi.net
Sat Apr 18 22:09:26 CEST 2009


On 2009.04.18 15:58:30, Jason Morgan wrote:
> On 2009.04.18 13:52:35, Serguei Kaniovski wrote:
> > I can generate the above dummies but can this design be imputed in a 
> > reg. model directly?

Oops, I apologize for not reading the whole question. Can you do the
following:

lm(y ~ I(ifelse(df$i=="AUT"|df$j=="AUT", 1, 0)) +
       I(ifelse(df$i=="BEL"|df$j=="BEL", 1, 0)) +
       I(ifelse(df$i=="GER"|df$j=="GER", 1, 0)), data=df)

If you exclude the ifelse(), you will get a vector of TRUE/FALSE,
which may or may not work.

~Jason

> Hello Serguei,
> 
> I am sure there is a better way to do this, but the following seems to
> work:
> 
> # Create sample data.frame()
> i  <- c("AUT", "AUT", "BEL")
> j  <- c("BEL", "GER", "GER")
> df <- data.frame(i=i, j=j)
> 
> # Create dummy vectors
> df$d.aut <- ifelse(df$i=="AUT"|df$j=="AUT", 1, 0)
> df$d.bel <- ifelse(df$i=="BEL"|df$j=="BEL", 1, 0)
> df$d.ger <- ifelse(df$i=="GER"|df$j=="GER", 1, 0)
> 
> # Print results
> df
> 
> HTH,
> 
> ~Jason
> 
> 

--
Jason W. Morgan
Graduate Student, Political Science
*The Ohio State University*




More information about the R-help mailing list