0% found this document useful (0 votes)
9 views12 pages

DBMS 1

This lab sheet introduces the R programming language and its computing environment, covering basic constructs like data types, control flow, loops, functions, and data structures such as vectors, matrices, and arrays. It includes examples of arithmetic operations, conditional statements, and user-defined functions, along with exercises for practical application. The document also outlines several programming tasks for students to complete, reinforcing their understanding of R.
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)
9 views12 pages

DBMS 1

This lab sheet introduces the R programming language and its computing environment, covering basic constructs like data types, control flow, loops, functions, and data structures such as vectors, matrices, and arrays. It includes examples of arithmetic operations, conditional statements, and user-defined functions, along with exercises for practical application. The document also outlines several programming tasks for students to complete, reinforcing their understanding of R.
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/ 12

19EAC385 R Programming Lab

Date: ..../....../......

Lab Sheet 1
Familiarization of R Computation
Environment
Aim
• To familiarize with the R computing environment and the basic constructs of
the R programming language, such as, data types, control flow, loops, func-
tions, strings, vectors, matrices and arrays.

Introduction
R is a programming language and a computation environment mainly used for sta-
tistical analysis, data visualization and reporting. It is a widely used programming
language in machine learning, statistics, and data analysis. Though R is a self con-
tained and independent programming language, it also allows us to integrate with
other languages such as C and C++. This labsheet serves as a basic introduction
to the R programming language and the R computation environment.

Examples
Basic Arithmetic in R
x = 2
y = 3
z = x + y
print ( z )

Figure 1: Basic Arithmetic in R

Data Types and Variables

Department of ECE 1 Sarang KP [AM.EN.U4EAC22061]


19EAC385 R Programming Lab

x = TRUE
print ( class ( x ) )

x = 1.234
print ( class ( x ) )

x = 2L
print ( class ( x ) )

x = 2+3 i
print ( class ( x ) )

x = ’c ’
print ( class ( x ) )

x = charToRaw (" Hello ")


print ( class ( x ) )

Figure 2: Data Types in R

Vectors
v = c (1 ,2 ,3)
print ( v )

v1 = c (1 ,4 ,9 ,16 ,25)
print ( v1 )

Department of ECE 2 Sarang KP [AM.EN.U4EAC22061]


19EAC385 R Programming Lab

Figure 3: Vectors

Matrices
M = matrix ( c (1 ,2 ,3 ,4 ,5 ,6) , nrow = 2 , ncol = 3 , byrow = TRUE )
print ( M )
M = matrix ( c (1 ,2 ,3 ,4 ,5 ,6) , nrow = 2 , ncol = 3 , byrow = FALSE )
print ( M )
M = matrix ( c (1 ,2 ,3 ,4 ,5 ,6) , nrow = 2 , ncol = 3)
print ( M )
I = matrix ( c (1 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,1) , nrow = 3 , ncol = 3)
print ( I )

Figure 4: Matrices

Arrays
A = array ( c (1 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,1 ,2 ,0 ,0 ,0 ,2 ,0 ,0 ,0 ,2) , dim = c (3 ,3 ,2) )
print ( A )

A1 = array ( c (1 ,0) , dim = c (3 ,3 ,2) )


print ( A1 )

A2 = array ( c (1 ,0 ,3) , dim = c (3 ,3 ,2) )


print ( A2 )

A3 = array ( c (1 ,0 ,3 ,4) , dim = c (3 ,3 ,2) )


print ( A3 )

Department of ECE 3 Sarang KP [AM.EN.U4EAC22061]


19EAC385 R Programming Lab

A4 = array ( c (1 ,0 ,3 ,4 ,5) , dim = c (3 ,3 ,2) )


print ( A4 )

Figure 5: Arrays

if...else statement
a = 14
if ( is . integer ( a ) ) {
print (" a is an integer ")
} else if ( is . numeric ( a ) ) {
print (" a is numeric ")
} else {
print (" a is neither integer nor numeric ")
}

x = runif (1 , min =0 , max =0)


if (x >0.5) {
print (" Success ")
} else {
print (" Failure ")
}

Figure 6: if...else statement

Department of ECE 4 Sarang KP [AM.EN.U4EAC22061]


19EAC385 R Programming Lab

Switch Statement
x = switch (" color " , " color "=" Red " , " shape "=" circle " ," type "=" good ")
print ( x )

Figure 7: Switch Statement

repeat statement
v = c (" Hello " ," Loop ")
counter = 1
repeat {
print ( v )
if ( counter > 5) {
break
}
counter = counter +1
}

Figure 8: repeat statement

x = 0
repeat {
print ( x )
x = x +1
if ( x > 10) {
break
}

Department of ECE 5 Sarang KP [AM.EN.U4EAC22061]


19EAC385 R Programming Lab

counter = 1
while ( counter <20) {
print ( counter )
counter = counter +2
}

Figure 9: repeat statement

Function
avg = mean (1:5)
print ( avg )

x1 = c (3 ,2 ,7 ,1 ,0 ,6 ,9 , -2 ,4)
print ( max ( x1 ) )
print ( min ( x1 ) )

x2 = sum (1:4)
print ( x2 )

print ( seq (1:5) )

Department of ECE 6 Sarang KP [AM.EN.U4EAC22061]


19EAC385 R Programming Lab

Figure 10: Function

User-defined Functions
printSq = function ( max_val ) {
for ( i in 1: max_val ) {
sq = i ^2
print ( sq )
}
return ( sq )
}
printSq (5)

Figure 11: User-defined Functions in R

Department of ECE 7 Sarang KP [AM.EN.U4EAC22061]


19EAC385 R Programming Lab

Exercise: Write an R function to return the mean of all inte-


gers between a minimum value and a maximum value. The
minimum and maximum values will be given as the input
arguments of the function. Print the returned mean value

calculate_mean = function ( min_val , max_val ) {


if ( min_val > max_val ) {
stop (" Minimum value should be less than or equal to maximum
value .")
}
mean_value = mean ( seq ( min_val , max_val ) )
return ( mean_value )
}

calculate_mean (5 , 10)

Figure 12: Function to Compute Mean

Question 1: Write a R program to take input from the user


(name and age) and display the values. Also print the version
of R installation.

Figure 13: Output

Department of ECE 8 Sarang KP [AM.EN.U4EAC22061]


19EAC385 R Programming Lab

Question 2:Write a R program to print the numbers from 1


to 100 and print ”Fizz” for multiples of 3, print ”Buzz” for
multiples of 5, and print ”FizzBuzz” for multiples of both.

Figure 14: Output

Question 3: Write a R program to create an empty data


frame

Figure 15: Output

Question 4: Write a R program to create two 2x3 matrix and


add, subtract, multiply and divide the matrixes.

Department of ECE 9 Sarang KP [AM.EN.U4EAC22061]


19EAC385 R Programming Lab

Figure 16: Output

Question 5: Write a R program to extract every nth element


of a given vector

Figure 17: Output

Question 6: What is the output of the function. Explain the


return() function

Figure 18: Output

The return () function explicitly specifies the value that the


function should return when called . In this case , the function
multiplies the input x by 5 and returns the result .

Department of ECE 10 Sarang KP [AM.EN.U4EAC22061]


19EAC385 R Programming Lab

Question 7: Explain the following function example

Figure 19: Output

If an argument is provided , the function uses that value .


If no argument is provided , it defaults to " Norway ".

Question 8: In the following example, what is the break func-


tion?

Figure 20: Output

The break function stops the loop once it encounters " cherry ".

Department of ECE 11 Sarang KP [AM.EN.U4EAC22061]


19EAC385 R Programming Lab

Question 9: Write a loop function to print “hello”, if the dice


number is less than 6.

Figure 21: Output

Question 10: Write a function to get user number and give


the square root of that number.

Figure 22: Output

Evaluation
Participation Knowledge Results Conduct Report Ethics Total

Name of the faculty:

Signature with date:

Department of ECE 12 Sarang KP [AM.EN.U4EAC22061]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy