R is a statistical analyzation language, and that means it is very good at data manipulation and data analyzation. One key way to analyze data is through plotting, and R excels in this field.
\[
y' = f(x,y), \qquad y(x_0 ) = y_0 .
\]
Two concatenate strings, use command paste
Extract substring of a string
Determine whether elements of a vector are in a set, and
give positions of corresponding elements in the set.
Then loc contains the locations of first occurences of elements
of x in the set y, and NA for unmatched elements.
Find indices of regular expression pattern p in string s
The returned vector also has a “match.length” attribute giving lengths of the
matches; this attribute can be removed via command
attributes(v)=NULL.)
Perform some commands only if the regular expression
p is contained in the string s:
if (grepl(p,s)) {
...commands...
}
Convert number to string
as.character(x)
Use sprintf to create a formatted string. Use %d for
integers (“d” stands for “decimal”, i.e. base 10), %f for
floating-point numbers, %e for scientific-notation floating
point, %g to automatically
choose %e or %f based on the value. You can specify
field-widths/precisions, e.g. %7d for integers with
padding to 7 spaces, or %.7f
for floating-point with 7 digits of precision. There are
many other options too.
Pause for x seconds
Sys.sleep(x)
R will wait until the user presses the Enter key
scan(quiet=TRUE)
Produce a beep (or possibly a visual signal, depending on
preferences set)
alarm()
Measure elapsed (“wall-clock”) time used to do some commands
t1=proc.time(); ...commands...;
(proc.time()-t1)
Print an error message an interrupt execution
stop(’Problem!’)
Print a warning message
warning(’Smaller problem!’)
When coding, separate statements by semicolons.
Evaluate contents of a string s as command(s).
eval(parse(text=s))
Get a command prompt for debugging, while executing a
script or function. While at
that prompt, you can type expressions to see the values of
variables, etc.
Insert the command browser() in
your file. Note that your prompt will
change to Browse[1]>. When you are
done debugging and want to continue
executing the file, either type c or just
press return (i.e. enter a blank line).
Note, if you type n, you enter the step debugger.
To install e.g. the deSolve package, you can use the command
install.packages(’deSolve’).
You then need to load the package
in order to use it, via the command library(’deSolve’). When running
R again later you’ll need to load the
package again to use it, but you should not need to re-install it. Note
that the lattice package is typically
included with binary distributions of
R, so it only needs to be loaded, not installed. Also, installing packages
with RStudio makes your life easier.