R Constant | Variables & Constants - r - learn r - r programming
Variables in R
- Variables are used to store data, whose value can be changed according to our need.
- Unique name given to variable (function and objects as well) is identifier.
Rules for writing Identifiers in R
- Identifiers can be a combination of letters, digits, period (.) and underscore (_).
- It must start with a letter or a period. If it starts with a period, it cannot be followed by a digit.
- Reserved words in R cannot be used as identifiers.
Valid identifiers in R
Invalid identifiers in R
Read AlsoC++ Global Variable
- Earlier versions of R used underscore (_) as an assignment operator. So, the period (.) was used extensively in variable names having multiple words.
- Current versions of R support underscore as a valid identifier but it is good practice to use period as word separators.
- For example, a.variable.name is preferred over a_variable_name or alternatively we could use camel case as aVariableName
Constants in R
- Constants are entities whose value cannot be altered. Basic types of constant are numeric constants and character constants.
- Numbers can be of type integer, double or complex.
- It can be checked with the typeof() function.
- Numeric constants followed by L are regarded as integer and those followed by i are regarded as complex.
- Numeric constants preceded by 0x or 0X are interpreted as hexadecimal numbers.
- Character constants can be represented using either single quotes (') or double quotes (") as delimiters.
Read AlsoGolang Const
- Some of the built-in constants defined in R along with their values is shown below.
- But it is not good to rely on these, as they are implemented as variables whose values can be changed.