How to write code expressions
In this section, we will discuss how to write various basic expressions which are the core elements of writing a program. Later, we will discuss how to create user-defined functions.
Expressions
R code consists of one or more expressions. An expression is an instruction to perform a particular task.
For example, the addition of two numbers is given by the following expression:
>4+5
It gives the following output:
[1] 9
If there is more than one expression in a program, they get executed one by one, in the sequence they appear.
Now we will discuss basic types of expressions.
Constant expression
The simplest form of expression are constant values, which may be character or numeric values.
For example, 100 is a numeric value expression of a constant value.
Hello World
is a character form expression of a constant expression.
Arithmetic expression
The R language has standard arithmetic operators and using these, arithmetic expressions can be written.
R has the following arithmetic operators:
Operands |
Operators |
|
Addition |
|
Subtraction |
|
Multiplication |
|
Division |
|
Exponentiation |
Using these arithmetic operations, one can generate arithmetic expressions; for example:
4+5 4-5 4*5
R follows the BODMAS rule. One can use parentheses to avoid ambiguity in creating any arithmetic expression.
Conditional expression
A conditional expression compares two values and returns a logical value in the form of True
or False
.
R has standard operators for comparing values and operators for combining conditions:
Operands |
Operators |
|
Equality |
|
Greater than (greater than equal to) |
|
Less than (less than equal to) |
|
Inequality |
|
Logical AND |
|
Logical OR |
|
Logical NOT |
For example:
10>5
, when executed, returns True
.
5>10
, when executed, returns False
.
Functional call expression
The most common and useful type of R expression is calling functions. There are a lot of built-in functions in R, and users can built their own functions. In this section, we will see the basic structure of calling a function.
A function call consists of a function name followed by parentheses. Within the parentheses, arguments are present, separated by commas. Arguments are expressions that provide the necessary information to the functions to perform the required tasks. An example will be provided when we discuss how to construct user-defined functions.
Symbols and assignments
R code consists of keywords and symbols.
A symbol is the label for an object stored in RAM, and it gets the stored value from the memory when the program gets executed.
R also stores many predefined values for predefined symbols, which is used in the program as required and gets automatically downloaded.
For example, the date()
function produces today's date when executed.
The result of an expression can be assigned to a symbol, and it is assigned by using the assignment operator <-
.
For example, the expression value <-4+6
assigns the symbol value with value 10
and is stored in memory.
Keywords
Some symbols are used to represent special values and cannot be reassigned:
NA
: This is used to define missing or unknown valuesInf
: This is used to represent infinity. For example, 1/0 produces the result infinityNaN
: This is used to define the result of arithmetic expression which is undefined. For example, 0/0 produces NaNNULL
: This is used to represent empty resultTRUE
andFALSE
: These are logical values and are generally generated when values are compared
Naming variables
When writing R code, we need to store various pieces of information under many symbols. So we need to name these symbols meaningfully as that will make the code easy to understand. Symbols should be self-explanatory. Writing short symbol name will make the code tougher to understand.
For example, if we represent date of birth information by DateOfBirth
or DOB
, then the first option is better as it is self-explanatory.