πŸ“Š
B.TECH III YEAR β€’ I SEMESTER β€’ R22

R Programming - Lab Manual

R Programming Laboratory β€’ Program Solution

πŸ“˜ R22 Regulation πŸŽ“ III Year πŸ“š I Semester πŸ“Š R Programming πŸ§ͺ Lab Program
Selected Program

R basics

← All R Programs

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 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.

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

👉  Rules for Naming Variables in R

Variable names in R must follow certain rules:

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

Used to perform mathematical operations:

OperatorNameExampleOutput
+Additionx + y10 + 5 β†’ 15
-Subtractionx - y10 - 5 β†’ 5
*Multiplicationx * y10 * 5 β†’ 50
/Divisionx / y10 / 4 β†’ 2.5
^Exponentiationx ^ y2 ^ 3 β†’ 8
%%Modulusx %% y15 %% 2 β†’ 1
%/%Integer Divisionx %/% y15 %/% 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:

OperatorNameExample
<-Left assignmentx <- 10
->Right assignment10 -> 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:

OperatorNameExample
==Equalx == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equalx >= y
<=Less than or equalx <= 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:

OperatorNameExample
&Element-wise Logical ANDx & y
&&Logical AND (short-circuit)x && y
|Element-wise Logical ORx | 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:

OperatorNameExample
:Sequence creationx <- 1:10
%in%Element belongs tox %in% y
%*%Matrix multiplicationMatrix1 %*% 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.

The compiler is embedded using OneCompiler. Your R program runs inside the embedded R environment; Study Glance does not execute R code directly on your web server. Programs that draw charts (pie, bar, box plots, histograms), read a CSV file from your own computer or install packages may not behave the same way in an online sandbox. Run those in RStudio or the R console.
← Back to R Programming Lab Programs πŸ“š All Lab Manuals