Building Formulas in R

In R it's often mandatory to use formulas. Here I will explain the basic syntax and give some tips how to write shorter formulas.

Basic Syntax

The independent is separated from the dependent ones by a tilde ~ and the dependent variables are separated by a plus +:

Y ~ X1 + X2 + X3

You can insert all variables of you dataset not mentioned so far with a single sign .:

So the same as above can be written as:

Y ~ .

If you want to use all variables except one this can be excluded with the - sign:

Y ~ . - X3

This is the same as Y ~ X1 + X2.

Dummy Variables

syntax : as.numeric(condition)

example: as.numeric(data$zeit >= 48)

categorial variables

syntax : as.factor(variable)

example: as.factor(cvar)

Interaction

syntax : variable:variable

example: var1:var2

The multiplication sign var1*var2 can be used as shortcut for var1 + var2 + var1:var2.