6
Flow Control
Both R
and MATLAB have similar and standard methods for controlling the flow of code — statements such as if/else, while, and for. The two packages are more similar than different, with mainly just some differences in the syntax used to delimit the loops.
6.1 Conditional (“if”) statements
Both platforms have if statements to allow a block of code to optionally be executed, depending on the value of a logical test. The common logical operators are listed in Table 6.1.
R
In R, an if statement has the form
R
if (condition) command
The condition should be a logical (TRUE/FALSE) value; unlike MATLAB, this should be a scalar value. If you provide a vector, only the first element is used, and a warning is displayed. Values ...
Get R and MATLAB now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.