R Commands

Some general properties of R commands

  1. R commands are case sensitive!
  2. Under Windows, paths to directories must not use the backslash, as in "C:\my-directory\my-subderictory\data-set". You have to use either the simple slash ("/") or double backslashes.

Some detail about commands

Assigning

Many commands assign something to an object. There are two assignment operators, <- and =:

sub1.xdata <- subset(xdata, inc > 1000)

sub1.xdata = subset(xdata, inc > 1000)

Both operators are equivalent. Note, however, that there is software with a language quite similar to R (such as OpenBUGS) that uses only the <- operator, so it may be worthwhile to stick with this.

Complex commands

Commands often consist of several elements, and these are typically separated by commas. For instance,

boxplot(y ~ x, data=mydata, notch=TRUE", col=c("gray60","gray40","gray20"))

requests a box-and-whisker-plot and indicates some options that influence the look of this plot.

Nesting

Commands can be nested. For instance, to compute correlations between two sets of variables, you may first create these sets and then compute the corrrelations:

mat1 <- cbind(mydata$var1, mydata$var2)
mat2 <- cbind(mydata$var3, mydata$var4)
cor(mat1, mat2)

But you may also put all these commands together in a single line; this is what I am referring to by 'nesting':

cor(cbind(mydata$var1, mydata$var2), cbind(mydata$var3, mydata$var4))

Model formulae

Many data modeling commands look like this:

lm(income ~ education + jobexp)

Such structures are called a formula: They relate a dependent variable, or response, to one or several independent variable(s), or predictor(s). The right hand side of the formula may look more complicated than it does in this short example; e.g., it may involve other symbols such as a * b which indicates an interaction, or a statement like- 1 which means that a model without intercept is to be estimated.

© W. Ludwig-Mayerhofer, R Guide | Last update: 09 Aug 2017