You are on page 1of 32

GLOCAL

UNIVERSITY
SCHOOL OF TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE


(B.Tech)
SEMESTER 7
ROLL NO: GU17R0462

Practical File of R Programming

SUBMITTED TO: SUBMITTED BY:


Mrs. Shagufta Praveen Mohd Shakir
(Asst. Professor)
ACKNOWLEDGEMENT

I Mohd Shakir from B.tech 4 th year(7th semester)of Glocal University have


prepared this file under the supervision of my Big Data Analytics
professor Shagufta Praveen
I wish to express my heartfelt gratitude to our H.O.D. Dr.
Mazhar for his generous consideration and blessings. I take
this opportunity to acknowledge my indebtedness for the kind
help extended to me in this endeavor by my friends.

I also wanted to thank my family members whose consistent


support has always encouraged me to take up challenges
boldly and complete them successfully.
CERTIFICATE

This to certify that the Practical File titled R programming Practical File submitted by Mohd
Shakir(GU17R0462) for the partial fulfillment of the requirements for the degree of bachelor of technology
(B.tech(C.S)) embodies the bonafide work done by him under the supervision.

Shagufta Parveen

Signature-

Place-
INDEX
Program Objective Remark
1. Vectors
Logical Vectors and operators
2.
3. Missing Values
4. Character vector exercises
5. Index Vector
6. Comparing vectors
7. Mode Exercises
8. Practical uses of R objects:some example
9. Factor Exercises
10. Using factor variable like a pro
11. Facing the facts about factors
12. Fighting factors with cats
13. Working with factors
14. Matrix Exercises
15. Array Exercises
16. Blind Exercises
17. Matrix Operations
18. List Exercises
19. Know your list
20. Data Frames
21. Merging Data Frames Exercises
22. Accessing Data Frame objects Exercises
23. Apply Functions to lists
Exercises based on vectors.

1. Vectors
# Creating a sequence from 5 to 13.
v <- 5:13
print(v)

# Creating a sequence from 6.6 to 12.6. v


<- 6.6:12.6
print(v)

# If the final element specified does not belong to the sequence then it is discarded. v
<- 3.8:11.4
print(v)
2)Logical Vectors and Operators
v <- c( 2,5.5,6) t
<- c(8, 3, 4)
print(v+t)

# AND logical Operators


v <- c(3,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
print(v&t) #OR
operators v <-
c(3,0,TRUE,2+2i) t <-
c(4,0,FALSE,2+3i)
print(v|t)
#Not Operators v <-
c(3,0,TRUE,2+2i)
print(!v)
3)Missing Values
x <- c(1:4, NA, 6:7, NA)

print(x)

#Find missing value is.na(x)

#replace missing value

x[is.na(x)] <- mean(x, na.rm = TRUE)

round(x, 2) print(x)

4)Character Vector Exercises


#Character Excerise

#Example 1 x <-

"Good Morning! "

print(nchar(x))

#Example-2

x <- c("Nature’s"," At its best ")

print(nchar(x)) #Example-3

fname <- "James" lname <-


"Bond" print(paste(fname,

lname))

#Example-4

print(vector(mode="character", length=10))

5)Index Vectors
s = c("aa", "bb", "cc", "dd", "ee") print(s[3])

#negative index

print(s[-3])
#Out-of-Range

print(s[10])
6)Comparing Vectors
#single value comparsion
x <- 9 y

<- 10

x == y

#multiple value comparsion


x <- c(1, 4, 9, 12) y

<- c(4, 4, 9, 13)

x == y

#Exact Equality (To test if two objects are exactly equal:)

x <- c(4, 4, 9, 12) y <- c(4, 4, 9, 12)

identical(x, y)

#Floating Point Comparison x

<- c(4.00000005, 4.00000008) y

<- c(4.00000002, 4.00000006)


all.equal(x, y)

#example-2

x <- c(4.005, 4.0008) y

<- c(4.002, 4.0006)

all.equal(x, y)

Exercises Object Modes and attributes 7)Mode Exercises


# Exercise 1

mode(c('a', 'b', 'c')) #Excercise

mode(3.32e16)

mode(sqrt(-2i))

mode(pressure)

#Excercise 3
x <- c('1', '2', '3')

y <- as.numeric(x) * 2 mode(y) <- 'character'

print(y)

8)Practical Uses of R objects: Some examples.


Ex. 1: Data cleaning
x <- c('20', '30', '4o')
# raw data,

x <- gsub('o', '0', x)


# cleaning

mode(x) <- 'numeric'


# convert from character to numeric

x+1
Ex-Checking input values mysum
<- function(a, b) {
if(is.numeric(a) & is.numeric(b)) a + b
else NA
}
mysum(10, 20)
## [1] 30
mysum(10, 'a')

#Ex. 3: Subsetting
df <- data.frame(years=1991:2010, v=sample(1:10, 20000, T))

mytable <- with(df, table(v, years))

# a cross-table with counts

mytable[, as.character(2005:2010)]
print(mytable)

10)using factor variables like a pro


# Plain old mtcars object print(mtcars)
# Looking at the mtcars object's attributes (metadata)

print(attributes(mtcars)) #excercise-2 numeric_var <-

c(1,2,3,4,5) factor_numeric_var <-

factor(numeric_var) print(attributes(numeric_var))

print(attributes(factor_numeric_var))

#excercise-3 character_var <- c("A", "B", "C",

"C", "C") factor_character_var <-

factor(character_var)

print(attributes(character_var))

## NULL print(attributes(factor_character_var))
11)facing the facts about factors
Exercise 1
Load the gapminder data-set from the gapminder package. Save it to an object called gp.
Check programmatically how many factors it contains and how many levels each factor has.
library(gapminder) gp
<- gapminder

# How many factors?


sum(sapply(gp, is.factor)) # How
many levels does each have?
lapply(Filter(is.factor, gp), nlevels)

Exercise 2

Notice that one continent, Antarctica, is missing from the corresponding factor – add it as the
last level of six.

attributes(gp$continent) levels(gp$continent) <-

c(levels(gp$continent), "Antartica")

attributes(gp$continent) table(gp$continent) Exercise 3

Actually, you change your mind. There is no permanent human population on Antarctica.
Drop this (unused) level from your factor. Can you find three ways to do this, then you are an
expert gp$continent <- factor(gp$continent)
12)fighting factors with cats
Exercise 1

Load the gapminder data-set from the gapminder package, as well as forcats. Check what the
levels of the continent factor variable are and their frequency in the data.

library(gapminder) library(forcats)

packageVersion("forcats") packageVersion("forcats" gp <-

gapminder fct_count(gp$continent) set.seed(5945)

paste(strsplit("forcats", "")[[1]][sample(1:7)], collapse = "")

Exercise 2

Notice that one continent, Antarctica, is missing – add it as the last level of six.

gp$continent <- fct_expand(gp$continent, "Antarctica")

# See how it changed: fct_count(gp$continent)


Exercise 3

Actually, you change your mind. There is no permanent human population on Antarctica.
Drop this (unused) level from your factor.

gp$continent <- fct_drop(gp$continent)

# See how it changed:

fct_count(gp$continent)

13)Working with factors


Exercise 1
Load the forcats package. Besides all the functions, there is a gss_cat data set in there. We will
use it for this exercise set.

Calculate the number of occurrences per level for the rincome column.

library(forcats)

fct_count(gss_cat$rincome)
Exercise 2
Re-order the levels of rincome by first occurrence of the level in the data-set.

fct_count(fct_inorder(gss_cat$rincome))

Exercise 3
Re-order the levels of rincome by frequency in the data-set.

fct_count(fct_infreq(gss_cat$rincome))
Exercise based on Arrays and Matrices 14)Matrix Excercises
R program to create a blank matrix.
m = matrix(, nrow = 10, ncol = 5)
print("Empty matrix of 10 rows and 5 columns:") print(m)

R program to create a matrix taking a given vector of numbers as input.


Display the matrix.
M = matrix(c(1:16), nrow = 4, byrow = TRUE)

print("Original Matrix:") print(M)

R program to create a matrix taking a given vector of numbers as input and define
the column and row names. Display the matrix
row_names = c("row1", "row2", "row3", "row4") col_names

= c("col1", "col2", "col3", "col4")

M = matrix(c(1:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))

print("Original Matrix:")

print(M)

15)Array Excercises
R program to convert a given matrix to a 1 dimensional array
m=matrix(1:12,3,4)

print("Original matrix:")

print(m) a = as.vector(m)
print("1 dimensional array:")

print(a)

R program to create an array of two 3x3 matrices each with 3 rows and 3
columns from two given two vectors.

print("Two vectors of different lengths:")

v1 = c(1,3,4,5) v2 = c(10,11,12,13,14,15)

print(v1) print(v2) result =

array(c(v1,v2),dim = c(3,3,2)) print("New

array:") print(result)

Write a R program to create an 3 dimensional array of 24 elements using the


dim() function
v = sample(1:5,24,replace = TRUE)
dim(v) = c(3,2,4) print("3-
dimension array:") print(v)
16)Bind Excercises
Exercise 1
Try to create matrices from the vectors below, by binding them column-wise. First, without
using R, write down whether binding the vectors to a matrix is actually possible; then the
resulting matrix and its mode (e.g., character, numeric etc.). Finally check your answer using
R.
a <- 1:5; b <- 1:5 m
<- cbind(a, b)
m
is.matrix(m)
mode(m)
a <- 1:5; b <- c('1', '2', '3', '4', '5')
m <- cbind(a, b)
m
is.matrix(m)
mode(m)
a <- 1:5; b <- 1:4; c <- 1:3 m
<- cbind(a, b)
m
is.matrix(m)
mode(m)

Exercise 2
Repeat exercise 1, binding vectors row-wise instead of column-wise while avoiding any row names.
a <- 1:5; b <- 1:5 m <- rbind(a, b,

deparse.level=0) is.matrix(m)

mode(m)
a <- 1:5; b <- c('1', '2', '3', '4', '5')

m <- rbind(a, b, deparse.level=0)

m
is.matrix(m)

mode(m)

a <- 1:5; b <- 1:4; c <- 1:3 m <-

rbind(a, b, deparse.level=0)

is.matrix(m)

mode(m)

Exercise 3
Bind the following matrices column-wise. First, without using R, write down whether binding
the matrices is actually possible; then the resulting matrix and its mode (e.g., character,
numeric etc.).
a <- matrix(1:12, ncol=4) ; b <- matrix(21:35, ncol=5)

nrow(a) == nrow(b) m <- cbind(a, b)

mode(m)

## [1] "numeric" a <- matrix(1:12, ncol=4) ; b <-

matrix(21:35, ncol=3) nrow(a) == nrow(b)

# cbind not possible due to different number of rows

## [1] FALSE a <- matrix(1:39, ncol=3) ; b <-

matrix(LETTERS, ncol=2) nrow(a) == nrow(b)

# to check if cbind is possible

m <- cbind(a, b)

mode(m)
17)Matrix Operation
Matrix Addition & Subtraction
A <- matrix(c(2,3,-2,1,2,2),3,2)

B <- matrix(c(1,4,-2,1,2,1),3,2)
C <- A + B

Print(c)

Exercises based on lists and data frames


18)List Exercises
R program to create a list containing strings, numbers, vectors and a logical values.
list_data = list("Python", "PHP", c(5, 7, 9, 11), TRUE, 125.17, 75.83)

print("Data of the list:") print(list_data)

R program to list containing a vector, a matrix and a list and give names to the
elements in the list
list_data <- list(c("Red","Green","Black"), matrix(c(1,3,5,7,9,11), nrow = 2),

list("Python", "PHP", "Java")) print("List:")

print(list_data) names(list_data) = c("Color", "Odd

numbers", "Language(s)") print("List with column names:")

print(list_data)

R program to create a list containing a vector, a matrix and a list and give names
to the elements in the list. Access the first and second element of the list.

list_data <- list(c("Red","Green","Black"), matrix(c(1,3,5,7,9,11), nrow = 2),

list("Python", "PHP", "Java")) print("List:")

print(list_data) names(list_data) = c("Color", "Odd

numbers", "Language(s)") print("List with column names:")

print(list_data) print('1st element:') print(list_data[1])

print('2nd element:') print(list_data[2])


19)Know your list
Exercise 1
Create a list called x with two elements; two vectors of length 1 called
a and b whose value is 1 and 2 respectively.
x <- list(a = 1, b = 2)

x
Exercise 2
Add a third vector of length 1 to x, z = 3.

x["c"] <- 3 x

Exercise 3
Turn your list into a named vector called y with only one line of code.

y <- unlist(x)
y

20)Data Frames.
# Create the data frame. emp.data

<- data.frame(

emp_id = c (1:5), emp_name =

c("Rick","Dan","Michelle","Ryan","Gary"), salary =

c(623.3,515.2,611.0,729.0,843.25),

start_date = as.Date(c("2012-01-01","2013-09-23","2014-11-15","2014-05-11",

"2015-03-27")),

stringsAsFactors = FALSE

# Extract Specific columns.

result <- data.frame(emp.data$emp_name,emp.data$salary)


print(result)
21)Merging Data Frames Objects Exercise

Exercise 1
Create the dataframes to merge: buildings <-
data.frame(location=c(1, 2, 3), name=c("building1",
"building2", "building3"))

data <- data.frame(survey=c(1,1,1,2,2,2), location=c(1,2,3,2,3,1),


efficiency=c(51,64,70,71,80,58))

The dataframes, buildings and data have a common key variable called, “location”.
Use the merge() function to merge the two dataframes by “location”, into a new
dataframe, “buildingStats”.

# Create the dataframes to merge:

buildings <- data.frame(location=c(1, 2, 3), name=c("building1", "building2", "building3"))

data <- data.frame(survey=c(1,1,1,2,2,2), location=c(1,2,3,2,3,1), efficiency=c(51,64,70,71,80,58))

# Solution buildingStats <- merge(buildings, data,

by="location")

Exercise 2
Give the dataframes different key variable names:
buildings <- data.frame(location=c(1, 2, 3), name=c("building1",
"building2", "building3"))
data <- data.frame(survey=c(1,1,1,2,2,2),
LocationID=c(1,2,3,2,3,1), efficiency=c(51,64,70,71,80,58))’

# Give the dataframes different key variable names: buildings <- data.frame(location=c(1, 2, 3),

name=c("building1", "building2", "building3")) data <- data.frame(survey=c(1,1,1,2,2,2),

LocationID=c(1,2,3,2,3,1), efficiency=c(51,64,70,71,80,58))

# Solution buildingStats <- merge(buildings, data,

by.x="location",

by.y="LocationID")

The dataframes, buildings and data now have corresponding variables


called, location, and LocationID. Use the merge() function to merge the
columns of the two dataframes by the corresponding variables.

Exercise 3 Inner
Join:
The R merge() function automatically joins the frames by common variable
names. In that case, demonstrate how you would perform the merge in Exercise 1
without specifying the key variable.

buildings <- data.frame(location=c(1, 2, 3), name=c("building1", "building2", "building3"))

data <- data.frame(survey=c(1,1,1,2,2,2), location=c(1,2,3,2,3,1), efficiency=c(51,64,70,71,80,58))

# Solution
buildingStats <- merge(buildings, data)
22)Accessing data frame objects exercises
Exercise 1 attach() – Attach a set of R Objects to
Search Path

Required Dataframe:
buildingSurvey <- data.frame(name=c("bldg1", "bldg2", "bldg3",
"bldg4", "bldg5", "bldg6"),
survey=c(1,1,1,2,2,2),
location=c(1,2,3,2,3,1), floors=c(5,
10, 10, 11, 8, 12),
efficiency=c(51,64,70,71,80,58))

Use the attach() function to make the variables


in "buildingSurvey" independently searchable. Then, use “summary()” to
create a summary of the “floors” variable.

attach(buildingSurvey) summary(floors)

Exercise 2
Using the “summary()” function, find the median “efficiency” value of
“buildingSurvey“, using objects in the R environment search path.

summary(efficiency)
Exercise 3
Once attached, in order to change the dataframe variable, use the assignment

operator “<<-“. For example: variable1 <<- log(variable1) Use “<<-” to

divide the “efficiency” category by 100.

efficiency <<- efficiency/100

23)Apply functions to lists.


Exercise 1

Using lapply(), find the length of list1‘s observations.

list1 <- list(observationA = c(1:5, 7:3), observationB=matrix(1:6, nrow=2))

lapply(list1, length)

Exercise 2

Using lapply(), find the sums of list1‘s observations.

lapply(list1, sum)
Exercise 3

Use lapply() to find the quantiles of list1.


lapply(list1, quantile)

Exercise 4

Find the classes of list1‘s sub-variables, with lapply() lapply(list1,

class)

You might also like