Creating R Variables
Creating R Variables
Variables are used to store data whose value can be changed. Variables in R can be created by assigning values using the assignment operator <-
or =
.
An identifier is a unique name given to a variable. The rules for creating identifiers are given below:
- The name can contain letters, numbers, periods, and underscores.
- A variable name must start with a letter or a period
- Variable names are case-sensitive (e.g., foo and Foo are different)
- Reserved words cannot be used.
- They cannot start with a digit.
Example of valid identifiers: sum, simple_interest, t5
Types of Variables
R automatically assigns the data type when creating variables. The common data types
are as follows:
Numeric: Real numbers (e.g., 3.14)
Integer: Whole numbers (e.g., 5)
Character: Text strings (e.g., “TestingDocs”)
Logical: Boolean values (TRUE, FALSE)
Complex: Complex numbers (e.g., 1+7i)