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:
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
.
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))
[1] 23
[1] 48.5
[1] "numeric"
[1] "numeric"
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
.
Filename: integer_d.R
# without decimals
age <- 23L
print(age)
# print data type of variables
print(class(age))
[1] 23
[1] "integer"
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
.
Filename: complex_d.R
val <- 5 + 6i
print(val)
# print data type of variables
print(class(val))
[1] 5+6i
[1] "complex"
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.
''
) for character values""
) for string valuesFilename: character_d.R
# create a string variable
fname <- "Apple"
print(class(fname))
# create a character variable
ch <- 'A'
print(class(ch))
[1] "character"
[1] "character"
The logical data type in R is also known as boolean data type. It can only have two values: TRUE
and FALSE
.
Filename: logical_d.R
b_val1 <- TRUE
print(b_val1)
print(class(b_val1))
b_val2 <- FALSE
print(b_val2)
print(class(b_val2))
[1] TRUE
[1] "logical"
[1] FALSE
[1] "logical"
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.
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
Filename: variables.R
# creating variables
sname <- "Naveen"
sage <- 20
# printing variables
sname
sage
[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.
print()
Function
R also provides the print()
function, which might feel more familiar if you're used to languages like Python.
Filename: variables_p.R
# creating variables
sname <- "Naveen"
sage <- 20
# printing variables
print(sname)
print(sage)
[1] "Naveen"
[1] 20
=
is used for assignment. In R, you can use both =
and <-
.<-
as some R contexts don't allow =
.print()
to display output. However, when inside expressions like { }
, print()
is recommended.Variable names in R must follow certain rules:
(.)
, and can be followed by letters, numbers, .
, or _
..
, it cannot be followed by a digit.(_)
.a
and A
are different).if
, TRUE
, NULL
) cannot be used.firstname <- "Naveen"
first_name <- "Naveen"
firstName <- "Madhu"
FIRSTNAME <- "Madhu"
name1 <- "Durga"
.fname <- "Durga"
first name <- "Naveen"
first-name <- "Naveen"
first@Name <- "Madhu"
_FNAME <- "Madhu"
1name <- "Durga"
.1name <- "Durga"
R allows assigning a single value to multiple variables in a single line.
Syntax:
var1 <- var2 <- var3 <- value
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)
[1] 10
[1] 10
[1] 10
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:
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 |
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")
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
Used to assign values to variables:
Operator | Name | Example |
---|---|---|
<- | Left assignment | x <- 10 |
-> | Right assignment | 10 -> x |
Filename: assign_op.R
x <- 30
print(x)
40 -> y
print(y)
[1] 30
[1] 40
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 |
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")
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
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 |
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")
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
Used for specific data manipulation:
Operator | Name | Example |
---|---|---|
: | Sequence creation | x <- 1:10 |
%in% | Element belongs to | x %in% y |
%*% | Matrix multiplication | Matrix1 %*% Matrix2 |
Filename: miscellaneous_op.R
x <- 1:10
print(x)
print(3 %in% x)
print(12 %in% x)
[1] 1 2 3 4 5 6 7 8 9 10
[1] TRUE
[1] FALSE
1. Download and install R-Programming environment and install basic packages using install. packages() command in R. View Solution
2. Learn all the basics of R-Programming (Data types, Variables, Operators etc,.) View Solution
3. Write R command to
i) Illustrate summation, subtraction, multiplication, and division operations on vectors using vectors.
ii) Enumerate multiplication and division operations between matrices and vectors in R console. View Solution
4. Write R command to
i) Illustrates the usage of Vector subsetting and Matrix subsetting
ii) Write a program to create an array of 3 X 3 matrixes with 3 rows and 3 columns. View Solution
5. Write an R program to draw i) Pie chart ii) 3D Pie Chart, iii) Bar Chart along with chart legend by considering suitable CSV file. View Solution
6. Create a CSV file having Speed and Distance attributes with 1000 records. Write R program to draw
i) Box plots
ii) Histogram
iii) Line Graph
iv) Multiple line graphs
v) Scatter plot
to demonstrate the relation between the cars speed and the distance. View Solution
7. Implement different data structures in R (Vectors, Lists, Data Frames). View Solution
8. Write an R program to read a csv file and analyze the data in the file using EDA (Explorative Data Analysis) techniques. View Solution
9. Write an R program to illustrate Linear Regression and Multi linear Regression considering suitable CSV file. View Solution