R basics
Aim:
☛ Learn all the basics of R-Programming (Data types, Variables, Operators etc,.).
Solution :
Datatypes in R:
In general, data types specify what type of data will be stored in variables. In other words, the variables can hold values of different data types.
In R, there is no need to specify the type of variable because the variable automatically changes its data type based on the assigned value.
R provides the class() function, which enables us to check the data type of a variable.
R has several basic data types, which include:
- numeric
- integer
- complex
- character (a.k.a. string)
- logical (a.k.a. boolean)
👉 Numeric Data Type :
The numeric data type in R is used to represent all real numbers, whether they have decimal points or not.
Examples include: 12, 15.6, 456, -78, -56.3.
Example:
Filename: numeric_d.R
# without decimals
age <- 23
print(age)
# with decimals
weight <- 48.5
print(weight)
# print data type of variables
print(class(age))
print(class(weight))
Output:
[1] 23
[1] 48.5
[1] "numeric"
[1] "numeric"
👉 Integer Data Type
The integer data type is used to represent real values without decimal points. We use the suffix L to specify integer type.
Examples: 45L, 123L, 78L, -45L.
Example
Filename: integer_d.R
# without decimals
age <- 23L
print(age)
# print data type of variables
print(class(age))
Output
[1] 23
[1] "integer"
👉 Complex Data Type
The complex data type is used to specify imaginary values in R. We use the suffix i to represent the imaginary part.
Examples: 3 + 2i, -2 + 5i.
Example
Filename: complex_d.R
val <- 5 + 6i
print(val)
# print data type of variables
print(class(val))
Output
[1] 5+6i
[1] "complex"
👉 Character Data Type
The character data type is used to represent character or string values in a variable.
In programming, a string is a set of characters. For example, 'A' is a character, and "Apple" is a string.
- Use single quotes (
'') for character values - Use double quotes (
"") for string values
Example
Filename: character_d.R
# create a string variable
fname <- "Apple"
print(class(fname))
# create a character variable
ch <- 'A'
print(class(ch))
Output
[1] "character"
[1] "character"
👉 Logical Data Type
The logical data type in R is also known as boolean data type. It can only have two values: TRUE and FALSE.
Example
Filename: logical_d.R
b_val1 <- TRUE
print(b_val1)
print(class(b_val1))
b_val2 <- FALSE
print(b_val2)
print(class(b_val2))
Output
[1] TRUE
[1] "logical"
[1] FALSE
[1] "logical"
Variables in R
A variable is a named memory location where we can keep values for a specific program. In simpler terms, a variable is a name that points to a memory location.
A variable is also called an identifier and is used to store a value.
👉 Creating Variables in R
In R, you do not need to declare a variable explicitly. When a value is assigned to a variable, it is automatically declared.
To assign a value to a variable, use the <- symbol. To print the variable value, just type the variable name.
Syntax:
variable_name <- value
Example:
Filename: variables.R
# creating variables
sname <- "Naveen"
sage <- 20
# printing variables
sname
sage
Output:
[1] "Naveen"
[1] 20
In the above example, sname and sage are variables, while "Naveen" and 20 are values.
In R, unlike other programming languages, you don't need to use a function to display variables. Simply writing the variable's name will display its value.
Using print() Function
R also provides the print() function, which might feel more familiar if you're used to languages like Python.
Example:
Filename: variables_p.R
# creating variables
sname <- "Naveen"
sage <- 20
# printing variables
print(sname)
print(sage)
Output:
[1] "Naveen"
[1] 20
Remainders
- In many programming languages,
=is used for assignment. In R, you can use both=and<-. - It is generally better to use
<-as some R contexts don't allow=. - You can optionally use
print()to display output. However, when inside expressions like{ },print()is recommended.
👉 Rules for Naming Variables in R
Variable names in R must follow certain rules:
- Must begin with a letter or a period
(.), and can be followed by letters, numbers,., or_. - If it starts with a
., it cannot be followed by a digit. - Cannot start with a number or an underscore
(_). - Variable names are case-sensitive (
aandAare different). - Reserved words (like
if,TRUE,NULL) cannot be used.
Valid Variable Names:
firstname <- "Naveen"
first_name <- "Naveen"
firstName <- "Madhu"
FIRSTNAME <- "Madhu"
name1 <- "Durga"
.fname <- "Durga"
Invalid Variable Names:
first name <- "Naveen"
first-name <- "Naveen"
first@Name <- "Madhu"
_FNAME <- "Madhu"
1name <- "Durga"
.1name <- "Durga"
👉 Multiple Variables
R allows assigning a single value to multiple variables in a single line.
Syntax:
var1 <- var2 <- var3 <- value
Example:
Filename: variables_m.R
# Assign one value to multiple variables in single line
a <- b <- c <- 10
# Print variable values
print(a)
print(b)
print(c)
Output:
[1] 10
[1] 10
[1] 10
Operators in R
In programming, an operator is a symbol that represents an action. In other words, it is used to perform operations on variables and values.
R supports the following operators:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Miscellaneous operators
👉 Arithmetic Operators
Used to perform mathematical operations:
| Operator | Name | Example | Output |
|---|---|---|---|
| + | Addition | x + y | 10 + 5 β 15 |
| - | Subtraction | x - y | 10 - 5 β 5 |
| * | Multiplication | x * y | 10 * 5 β 50 |
| / | Division | x / y | 10 / 4 β 2.5 |
| ^ | Exponentiation | x ^ y | 2 ^ 3 β 8 |
| %% | Modulus | x %% y | 15 %% 2 β 1 |
| %/% | Integer Division | x %/% y | 15 %/% 2 β 7 |
Example:
Filename: arith_op.R
# Define two numbers
a <- 15
b <- 2
add_result <- a + b
cat("Addition (a + b):", add_result, "\n")
sub_result <- a - b
cat("Subtraction (a - b):", sub_result, "\n")
mul_result <- a * b
cat("Multiplication (a * b):", mul_result, "\n")
div_result <- a / b
cat("Division (a / b):", div_result, "\n")
exp_result <- a ^ b
cat("Exponentiation (a ^ b):", exp_result, "\n")
mod_result <- a %% b
cat("Modulus (a %% b):", mod_result, "\n")
int_div_result <- a %/% b
cat("Integer Division (a %/% b):", int_div_result, "\n")
Output:
Addition (a + b): 17
Subtraction (a - b): 13
Multiplication (a * b): 30
Division (a / b): 7.5
Exponentiation (a ^ b): 225
Modulus (a %% b): 1
Integer Division (a %/% b): 7
👉 Assignment Operators
Used to assign values to variables:
| Operator | Name | Example |
|---|---|---|
<- | Left assignment | x <- 10 |
-> | Right assignment | 10 -> x |
Example:
Filename: assign_op.R
x <- 30
print(x)
40 -> y
print(y)
Output:
[1] 30
[1] 40
👉 Comparison / Relational Operators
Used to compare two values:
| Operator | Name | Example |
|---|---|---|
| == | Equal | x == y |
| != | Not equal | x != y |
| > | Greater than | x > y |
| < | Less than | x < y |
| >= | Greater than or equal | x >= y |
| <= | Less than or equal | x <= y |
Example:
Filename: relational_op.R
a <- 10
b <- 20
cat("a = ", a, "\n")
cat("b = ", b, "\n")
cat("Is a equal to b? :", (a == b), "\n")
cat("Is a not equal to b? :", (a != b), "\n")
cat("Is a greater than b? :", (a > b), "\n")
cat("Is a less than b? :", (a < b), "\n")
cat("Is a greater than or equal to b? :", (a >= b), "\n")
cat("Is a less than or equal to b? :", (a <= b), "\n")
Output:
a = 10
b = 20
Is a equal to b? : FALSE
Is a not equal to b? : TRUE
Is a greater than b? : FALSE
Is a less than b? : TRUE
Is a greater than or equal to b? : FALSE
Is a less than or equal to b? : TRUE
👉 Logical Operators
Used for combining multiple conditions:
| Operator | Name | Example |
|---|---|---|
| & | Element-wise Logical AND | x & y |
| && | Logical AND (short-circuit) | x && y |
| | | Element-wise Logical OR | x | y |
| || | Logical OR (short-circuit) | x || y |
| ! | Logical NOT | !x |
Example:
Filename: logical_op.R
x <- TRUE
y <- FALSE
cat("Logical AND (x & y):", x & y, "\n")
cat("Logical OR (x | y):", x | y, "\n")
cat("Logical NOT (!x):", !x, "\n")
cat("Short-circuit AND (x && y):", x && y, "\n")
cat("Short-circuit OR (x || y):", x || y, "\n")
Output:
Logical AND (x & y): FALSE
Logical OR (x | y): TRUE
Logical NOT (!x): FALSE
Short-circuit AND (x && y): FALSE
Short-circuit OR (x || y): TRUE
👉 Miscellaneous Operators
Used for specific data manipulation:
| Operator | Name | Example |
|---|---|---|
| : | Sequence creation | x <- 1:10 |
| %in% | Element belongs to | x %in% y |
| %*% | Matrix multiplication | Matrix1 %*% Matrix2 |
Example:
Filename: miscellaneous_op.R
x <- 1:10
print(x)
print(3 %in% x)
print(12 %in% x)
Output:
[1] 1 2 3 4 5 6 7 8 9 10
[1] TRUE
[1] FALSE
π» Run This R Program
R basics
The selected R program is loaded
automatically into the editor.
Edit the program, provide input if required
and click Run.