Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

BAHRIA UNIVERSITY, ISLAMABAD

Department of Computer Science

Artificial Intelligence Lab


Lab Journal 14
Student Name: Syed Haider Ali

Enrolment No.: 01-235181-083


Class and Section: 6A

Title: JumpStart to the world of R programming for Data Science

Objectives: To study and learn R Programming Language


Tools Used: R 4.0.3 and R Studio 1.3.1

Procedure: Open R Studio and perform the following tasks

Task # 1:
Make an R script for your first R program. Make a simple hello world program. Your program will
take name and age of user as input and store them in variable userName and userAge. Then it will
display the following line on screen (incremented user age)
Hello (userName)! You will be (userAge+1) years old next year.
Run your script through sourcing, as taught in tutorial given to you. Do not run line by line. (See the
hello world tutorial provided to you for this task)

Task # 2:
Open R Studio and run the following R programs. Try different values for separate runs of all
programs. Play around with the and run it again. See what happens. Make a note of what changes
you made and how it made the program behave. Also, if there are any errors, correct them and
write changes you made to remove the errors with output.

Run these in R console, not through a script.

# Create two vectors x and y of same size.


# Perform following operations on them

Artificial Intelligence Lab Designed by Umarah Qaseem


!x

xor(x, y)

Output:

#Method 1 for generating random numbers


Random numbers from a normal distribution can be generated using runif() function.
We need to specify how many numbers we want to generate, we can specify the range of
the uniform distribution using max and min argument, the default range is between 0 and
1.

> runif(1) # generates 1 random number


> runif(3) # generates 3 random number
> runif(3, min=5, max=10) # define the range between 5 and 10

Output:

#Method 2 for generating random numbers


Random numbers from a normal distribution can be generated using rnorm() function.
We need to specify the number of samples to be generated.
We can also specify the mean and standard deviation of the distribution, which are by
default 0 mean and 1 standard deviation.
> rnorm(1) # generates 1 random number
> rnorm(3) # generates 3 random number
> rnorm(10) # How many numbers should be generated now? Ans:
> rnorm(3, mean=10, sd=2) # provide our own mean and standard deviation

Output:

# Here are some built-in constants in R which you can use if you want. Run the following
commands and see what their output is.

> LETTERS
> letters
> pi
> month.name
> month.abb

Output:

Artificial Intelligence Lab Designed by Umarah Qaseem


# When you are working with a dataset many times there are NA or NaN values present in
the vectors which cause problem, functions such as sum(), mean(), prod() etc. produce NA
or NaN result on those vectors respectively.
In order to ignore such values, we pass in the argument na.rm = TRUE.

> sum(2,7,5)
> x=c(2,NA,3,1,4)
> sum(x)
> sum(x, na.rm=TRUE) # this way we can ignore NA and NaN values

Output:

Create scripts for all the following tasks and source them for running. Paste code below the task
as well as snapshot of output.

Task # 3:
Create a vector of 15 elements. It must have two NA values. (see last part of task 2 for help) Do the
following with that vector.
a) Find out its sum, mean and prod using the following functions. sum() mean() prod(). Make
sure the result is not NA.
b) Find out the maximum and minimum value of the vector and the indexes of each. Also find
the range of the vector. For all operations use the following functions. min(), max(), range(),
which.min(), which.max().
c) Sorting of vectors can be done using the sort() function. By default, it sorts in ascending
order. To sort in descending order, we can pass decreasing=TRUE. Sort the vector in
ascending as well as descending order. Display the vector before sorting and display result
after sorting on screen. Was the original vector changed when your sorted?

vec=c(4,2,5,3,5,3,NA,43,6,3,44,NA,3,5,3)
print(paste("Sum: ",sum(vec,na.rm = TRUE)))
print(paste("Product: ",prod(vec,vec,na.rm = TRUE)))
print(paste("Mean: ",mean(vec,na.rm = TRUE)))
print(paste("Max: ",max(vec,na.rm = TRUE)))
print(paste("Min: ",min(vec,na.rm = TRUE)))
print(paste("Max Index: ",which.max(vec)))
print(paste("Min Index: ",which.min(vec)))
print(range(vec,na.rm = TRUE))
print(vec)
print(sort(vec,decreasing = FALSE))
print(sort(vec,decreasing = TRUE))

Artificial Intelligence Lab Designed by Umarah Qaseem


Task # 4:
Write code and perform any one example from each of the OPERATORS tutorials (provided to you)
for scaler, vector and matrix.

a=5
b=3
print(a*b)
print(a>b)

a1=c(3,2,4)
b1=c(4,7,8)
print(a1+b1)
print(a1<b1)

a3=matrix(c(1:9),nrow = 3,ncol = 3,byrow = TRUE)


b3=matrix(c(1:9),nrow = 3,ncol=3,byrow = TRUE)
print(a3>b3)
print(a3+b3)

Artificial Intelligence Lab Designed by Umarah Qaseem


Task # 5:
Take two matrices from user.
Perform matrix multiplication on them.
Tell the user about the rule of matrix multiplication regarding order, before taking input. Display
the output matrix on screen.

Submission Date: Instructor: Ms. Umarah Qaseem

Artificial Intelligence Lab Designed by Umarah Qaseem

You might also like