0% found this document useful (0 votes)
7 views6 pages

R Lab

Uploaded by

shaimanoorbin231
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

R Lab

Uploaded by

shaimanoorbin231
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

R lab Ex3:

Develop a program to create two 3 X 3 matrices A and B and perform the following operations a)
Transpose of the matrix b) addition c) subtraction d) multiplication

A <- matrix(c(10, 8,5, 12), ncol = 2, byrow = TRUE)


A

B <- matrix(c(5, 3,15, 6), ncol = 2, byrow = TRUE)


B

Output:
#A #B
[, 1] [, 2] [, 1] [, 2]
[1, ] 10 8 [1, ] 5 3
[2, ] 5 12 [2, ] 15 6

These matrices are both of the same dimensions. You can check the dimensions (number of rows and
columns, respectively) of a matrix with the dim function.

dim(A) # 2 2
dim(B) # 2 2

a) Transpose of the matrix:

> t(A)
[,1] [,2]
[1,] 10 5
[2,] 8 12
> t(B)
[,1] [,2]
[1,] 5 15
[2,] 3 6

b) addition:

Addition of A & B:

A+B

> A+B
[,1] [,2]
[1,] 15 11
[2,] 20 18
KIT/CSE/ BDS306C/M5/BJ DATA ANALYTICS WITH R Page 41
Subtraction of B from A:

A-B

> A-B
[,1] [,2]
[1,] 5 5
[2,] -10 6

Matrix multiplication in R:

Before multiplying two matrices check that the dimensions are compatible. The number of columns of
the first matrix must be equal to the number of rows of the second.

A %*% B

> A %*% B
[,1] [,2]
[1,] 170 78
[2,] 205 87

Element-wise multiplication:
The element-wise multiplication of two matrices of the same dimensions can also be computed with the *
operator. The output will be a matrix of the same dimensions of the original matrices.

A*B

> A*B
[,1] [,2]
[1,] 50 24
[2,] 75 72

KIT/CSE/ BDS306C/M5/BJ DATA ANALYTICS WITH R Page 42


> A <- matrix(c(10, 8,5, 12), ncol = 2, byrow = TRUE)
> A
[,1] [,2]
[1,] 10 8
[2,] 5 12
>
> B <- matrix(c(5, 3,15, 6), ncol = 2, byrow = TRUE)
> B
[,1] [,2]
[1,] 5 3
[2,] 15 6
>
>
> t(A)
[,1] [,2]
[1,] 10 5
[2,] 8 12
> t(B)
[,1] [,2]
[1,] 5 15
[2,] 3 6
> A+B
[,1] [,2]
[1,] 15 11
[2,] 20 18
> A-B
[,1] [,2]
[1,] 5 5
[2,] -10 6
> A %*% B
[,1] [,2]
[1,] 170 78
[2,] 205 87
> A*B
[,1] [,2]
[1,] 50 24
[2,] 75 72

R lab Ex4:
Develop a program to find the factorial of given number using recursive function calls.

Example of Recursion
An example can help clarify the concept of recursion. Let us take the example of finding the factorial of a
number.

Factorial of a positive integer number is defined as the product of all the integers from 1 to that number.
For example, the factorial of 5 (denoted as 5!) will be:

KIT/CSE/ BDS306C/M5/BJ DATA ANALYTICS WITH R Page 43


5! = 1*2*3*4*5 = 120

This problem of finding factorial of 5 can be broken down into a sub-problem of multiplying the factorial
of 4 with 5.

5! = 5*4!

More generally,

n! = n*(n-1)!
Now we can continue this until we reach 0! which is 1.

Recursive Function in R:

recursive.factorial <- function(x) {


if (x == 0)
return(1)
else
return(x * recursive.factorial(x-1))
}

# factorial of 0
recursive.factorial(0)
# factorial of 5
recursive.factorial(5)
# factorial of 7
recursive.factorial(7)

> recursive.factorial <- function(x) {


+ if (x == 0)
+ return(1)
+ else
+ return(x * recursive.factorial(x-1))
+ }
>
> # factorial of 0
> recursive.factorial(0)
[1] 1
> # factorial of 5
> recursive.factorial(5)
[1] 120
> # factorial of 7
> recursive.factorial(7)
[1] 5040
> recursive.factorial(10)
[1] 3628800

KIT/CSE/ BDS306C/M5/BJ DATA ANALYTICS WITH R Page 44


R lab Ex5:

Develop an R Program using functions to find all the prime numbers up to a specified number by the
method of Sieve of Eratosthenes.

# Function to implement the Sieve of Eratosthenes


sieve_of_eratosthenes <- function(n) {
# Create a logical vector of TRUE values
primes <- rep(TRUE, n)
primes[1] <- FALSE # 1 is not a prime number

# Implement the sieve


for (i in 2:sqrt(n)) {
if (primes[i]) {
primes[seq(i^2, n, i)] <- FALSE
}
}

# Return the indices of the TRUE values, which are the prime numbers
which(primes)
}

# Function to find all prime numbers up to a specified number


find_primes <- function(n) {
if (n < 2) {
return(integer(0)) # Return an empty vector if n is less than 2
}
sieve_of_eratosthenes(n)
}

# Example usage
specified_number <- 50
prime_numbers <- find_primes(specified_number)
print(prime_numbers)

# Function to implement the Sieve of Eratosthenes


> sieve_of_eratosthenes <- function(n) {
+ # Create a logical vector of TRUE values
+ primes <- rep(TRUE, n)
+ primes[1] <- FALSE # 1 is not a prime number
+
+ # Implement the sieve
+ for (i in 2:sqrt(n)) {
+ if (primes[i]) {
+ primes[seq(i^2, n, i)] <- FALSE
+ }
+ }
+

KIT/CSE/ BDS306C/M5/BJ DATA ANALYTICS WITH R Page 45


+ # Return the indices of the TRUE values, which are the prime
numbers
+ which(primes)
+ }
>
> # Function to find all prime numbers up to a specified number
> find_primes <- function(n) {
+ if (n < 2) {
+ return(integer(0)) # Return an empty vector if n is less
than 2
+ }
+ sieve_of_eratosthenes(n)
+ }
>
> # Example usage
> specified_number <- 50
> prime_numbers <- find_primes(specified_number)
> print(prime_numbers)
[1] 2 3 5 7 11 13 17 19 23 29 31 37 41 43
[15] 47

This program defines two functions: sieve_of_eratosthenes(n): Implements the Sieve of Eratosthenes
algorithm to find all prime numbers up to n.

find_primes(n): Calls the sieve function and handles edge cases where n is less than 2.
You can change the specified_number variable to any positive integer to find all prime numbers up to that
number.

KIT/CSE/ BDS306C/M5/BJ DATA ANALYTICS WITH R Page 46

You might also like