Scripts

Scripts are collections of R commands that may be saved and used again later. In other words, they are equivalents of SPSS's 'syntax files' or Stata's 'do files'. Here are a few things you might wish to know.

Comments

Comments are part of a script that contain no executable commands; rather, they explain your work.

Comments start with an #. The remainder of the line is then ignored. Thus you may write:

nsim <- 500 # Change number of simulations as appropriate

Or you may use an entire line for a comment:

# This section prepares the data for the final analysis

Organizing commands

In the simplest case, you will use one line of code for each command. However, you may split commands of several lines of code. R will normally 'see' whether or not a command is finished at the end of a line. For instance, when writing

rmritems=list("n.obs", "n.vectors", "dim.items", "small.x", "large.x", "a", "b",
     "z1", "z2", "z3", "z4", "z5", "z6", "z7")

R will recognize that the command is not completed at the end of the first line, as the closing parenthesis is still missing.

On the other hand, in case you want to put two or more commands in a single line, you may do so by just separating them with a semicolon.

© W. Ludwig-Mayerhofer, R Guide | Last update: 27 Dec 2016