0% found this document useful (0 votes)
2 views37 pages

R20 Rprogramming Lab Manual

The document is a lab manual for R programming, detailing the installation process for R and RStudio, followed by a series of 25 experiments. Each experiment includes a specific task, source code, and expected output related to R programming concepts such as data frames, matrices, and basic operations. The manual serves as a practical guide for users to learn and apply R programming skills.

Uploaded by

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

R20 Rprogramming Lab Manual

The document is a lab manual for R programming, detailing the installation process for R and RStudio, followed by a series of 25 experiments. Each experiment includes a specific task, source code, and expected output related to R programming concepts such as data frames, matrices, and basic operations. The manual serves as a practical guide for users to learn and apply R programming skills.

Uploaded by

jyothicsediet
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

A LAB MANUAL

ON
R PROGRAMMING
LAB
(R20)
INSTALLATION OF R
STEP-1: Downloading and Installation of R:
i)Open an internet browser and go to [Link].
ii)Click the “download R” link in the middle of the page
under “Getting Started”.
iii) Select a CRAN location (a mirror site) and click the
corresponding link. Click on the “Download R for Windows”
link at the top of the page. Select the appropriate link for
downloading and click download.
iv) Once the download gets finished open it in the folder where
it has been downloaded, right-click and run it as administrator
to start the setup wizard.

v) Now you’ll be asked to select your preferred installation


language, here we will select English and click on the ok
button.
vi) Now some information about R will be displayed on the
screen, so here as well click on next.
vii)Next we’ll have to select the installation folder of R, we will
stick to the default installation location so simply click next.
But before that make sure you have at least 25Mb of free disk
space available.
viii)Now in the component selection window as well click on
next.
ix) In the startup options window we will go with the
default selected option, so simply click next.
x) Now you’ll be asked to select the start menu folder, so here
as well click
on the next button.
xi) In the select additional task window check 1,3,4 check box
as shown in the image below and click on the next button, now
the installation will begin and it may take some time so keep
patience.
xii) Once installation gets successful click on the finish
button to close the setup wizard.

STEP-2:Downloading and Installation of RStudio:


i) click on the “RStudio Desktop” and you’ll be
redirected to the downloads page of RStudio.
Download RStudio
Here: [Link]

ii) Click on the “Download RStudio For Windows”


button and your download will start.
iii) Once the download gets finished open it in the folder
where it has been downloaded, right-click and run it as
administrator to install the RStudio setup wizard.
iv) In the setup wizard’s welcome screen click on next.
v) Now we have to select the installation location for Rstudio,
we’ll stick
to the default installation location so click on the Next button.
vi) In the choose start menu window simply click on the
install button and your desired RStudio installation will
begin.

vii)Once installation gets complete click on the finish button


and you’re
all done.
To start Rstudio simply search Rstudio in the windows
search bar and press enter.
EXPERIMENT – 01
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.

Source Code:
name <- readline(prompt = “Enter your
name :”) age <- readline(prompt = “Enter
your age:”) print(paste("Hi,", name, "I am
", age, "years old."))
print([Link])
Sample output:

EXPERIMENT – 02

2. Write a R program to get the details of the objects in


memory.

Source Code:
name <-
"Python"; n1 <-
10;
n2 <- 0.5
nums <- c(10, 20, 30, 40, 50, 60)
print(ls())
print("Details of the objects in
memory:") print([Link]())
Sample output:
EXPERIMENT – 03
3. Write a R program to create a sequence of numbers
from 20 to 50 and find the mean of numbers from 20 to
60 and sum of numbers from 51 to 91.
Source Code:
me <-
mean(20:60)
sum <-
sum(51:91)
print("Sequence of numbers from 20
to 50:") print(seq(20,50))
print(paste("Mean of numbers from 20 to
60:",me)) print(paste("Sum of numbers
from 51 to 91:",sum)) Sample output:
EXPERIMENT – 04

4. Write a R program to create a simple bar plot of five


subjects marks.
Source code:
marks = c(70, 95, 80, 74,64)
barplot(marks,
main = "Comparing marks of 5 subjects",
xla = "Marks",
b = "Subject",
yla
[Link] = c("English", "Science", "Math.", "Hist.",
"Communication"), col = "blue",
horiz = FALSE)
Sample output:
EXPERIMENT – 05
5. Write a R program to get the unique elements of a
given string and unique numbers of vector.
Source code:
str1 = "The quick brown fox jumps over the
lazy dog." print(paste("Original
vector(string)",str1)) print("Unique elements
of the said vector:")
print(unique(tolower(str1)))
nums = c(1, 2, 2, 3, 4, 4, 5, 6)
print("Original
vector(number)")
print(nums)
print(paste("Unique elements of the said
vector:",unique(nums))) Sample output:
EXPERIMENT – 06
6. Write a R program to create three vectors a,b,c with 3
integers. Combine the three vectors to become a 3×3
matrix where each column represents a vector. Print the
content of the matrix.
Source code:
a<-c(1,2,3)
b<-c(4,5,6)
c<-c(7,8,9)
m<-cbind(a,b,c)
print("Content of the said
matrix:") print(m)
Sample output:

EXPERIMENT – 07
7. Write a R program to create a 5 x 4 matrix , 3 x 3 matrix
with labels and fill the matrix by rows and 2 × 2 matrix
with labels and fill the matrix by columns.
Source code:
m1 = matrix(1:20, nrow=5,
ncol=4) print("5 × 4
matrix:")
print(m1)
cells = c(1,3,5,7,8,9,11,12,14)
rnames = c("Row1", "Row2", "Row3")
cnames = c("Col1", "Col2", "Col3")
m2 = matrix(cells, nrow=3, ncol=3, byrow=TRUE,
dimnames=list(rnames, cnames))
print("3 × 3 matrix with labels, filled by
rows: ") print(m2)
print("3 × 3 matrix with labels, filled by columns: ")
m3 = matrix(cells, nrow=3, ncol=3, byrow=FALSE,
dimnames=list(rnames, cnames))
print(m3)
Sample
output:

EXPERIMENT – 08
8. Write a R program to combine three arrays so that the
first row of the first array is followed by the first row of
the second array and then first row of the third array.
Source Code:
num1 = rbind(rep("A",3), rep("B",3),
rep("C",3)) print("num1")
print(num1)
num2 = rbind(rep("P",3), rep("Q",3),
rep("R",3)) print("num2")
print(num2)
num3 = rbind(rep("X",3), rep("Y",3),
rep("Z",3)) print("num3")
print(num3)
a = matrix(t(cbind(num1,num2,num3)),ncol=3, byrow=T)
print("Combine three arrays, taking one row from each
one by one:") print(a)
Sample output:

EXPERIMENT – 09
9. Write a R program to create a two-dimensional 5x3
array of sequence of even integers greater than 50.
Source Code:
a <- array(seq(from = 50, [Link] = 15, by
= 2), c(5, 3)) print("Content of the array:")
print("5×3 array of sequence of even integers
greater than 50:") print(a)
Sample output:
EXPERIMENT – 10
10. Write a R program to create an array using four given
columns, three given rows, and two given tables and
display the content of the array.
Source Code:
array1 = array(1:30, dim=c(3,5,2))
print(array1)
Sample output:

EXPERIMENT – 11
11. Write a R program to create an empty data frame.
Source Code:
df = [Link](Ints=integer(),
Doubles=double(),
Characters=character(),
Logicals=logical(),
Factors=factor(),
stringsAsFactors=FALSE)
print("Structure of the empty
dataframe:") print(str(df))
Sample Output:

EXPERIMENT – 12
12. Write a R program to create a data frame from
four given vectors.
Source code:
name = c('Anastasia', 'Dima', 'Katherine', 'James',
'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas')
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19)
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1)
qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes',
'no', 'no', 'yes') print("Original data frame:")
print(name)
print(score)
print(attemp
ts)
print(qualify)
df = [Link](name, score, attempts,
qualify) print(df)
Sample output:

EXPERIMENT – 13
13. Write a R program to create a data frame using two
given vectors and display the duplicated elements and
unique rows of the said data frame.
Source code:
a = c(10,20,10,10,40,50,20,30)
b = c(10,30,10,20,0,50,30,30)
print("Original data
frame:") ab =
[Link](a,b)
print(ab)
print("Duplicate elements of the said data
frame:") print(duplicated(ab))
print("Unique rows of the said data
frame:") print(unique(ab))
Sample output:

EXPERIMENT – 14
14. Write a R program to save the information of a data
frame in a file and display the information of the file.
Source Code:
exam_data = [Link](
name = c('Anastasia', 'Dima', 'Katherine', 'James',
'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes')
)
print("Original
dataframe:")
print(exam_data)
save(exam_data,file="data
.rda") load("[Link]")
[Link]("[Link]")
Sample output:

EXPERIMENT – 15
15. Write a R program to create a matrix from a list
of given vectors.
Source code:
l = list()
for (i in 1:5) l[[i]] <-
c(i, 1:4) print("List of
vectors:") print(l)
result =
[Link](rbind, l)
print("New
Matrix:")
print(result)
Sample output:

EXPERIMENT – 16
16. Write a R program to concatenate two given matrices
of same column but different rows.
Source code:
x = matrix(1:12, ncol=3)
= matrix(13:24,
y
ncol=3)
print("Matrix-1")
print(x)
print("Matrix-
2") print(y)
result = dim(rbind(x,y))
print("After concatenating two given
matrices:") print(result)
Sample output:

EXPERIMENT – 17
17. Write a R program to find row and column index of
maximum and minimum value in a given matrix.
Source code:
m = matrix(c(1:16), nrow = 4, byrow
= TRUE) print("Original Matrix:")
print(m)
result = which(m == max(m), [Link]=TRUE)
print("Row and column of maximum value of the said
matrix:") print(result)
result = which(m == min(m), [Link]=TRUE)
print("Row and column of minimum value of the said
matrix:") print(result)
Sample output:

EXPERIMENT – 18
18. Write a R program to append value to a given empty
vector.
Source code:
()vecto
(0,1,2,3,4,5,6,7,8,9)
=c
r
1:length(values))
value =c[i] <-
s
values[i]
fo (i in
print(vector)

Sample output:
EXPERIMENT – 19
19. Write a R program to multiply two vectors of integers
type and length 3.
Source code:
x = c(10, 20, 30)
y = c(20, 10, 40)
print("Original
Vectors:") print(x)
print(y)
print("Product of two Vectors:")
z=x*
y
print(z
)
Sample output:

EXPERIMENT – 20
20. Write a R program to find Sum, Mean and Product of
a Vector, ignore element like NA or NaN.
Source code:
x = c(10, NULL, 20, 30, NA)
print("Sum:")
#ignore NA and NaN
values print(sum(x,
[Link]=TRUE))
print("Mean:")
print(mean(x,
[Link]=TRUE))
print("Product:")
print(prod(x,
[Link]=TRUE))
Sample output:

EXPERIMENT – 21
21. Write a R program to list containing a vector, a matrix
and a list and give names to the elements in the list.
Source code:
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_dat
a)
names(list_data) = c("Color", "Odd numbers",
"Language(s)") print("List with column names:")
print(list_data)
Sample output:
EXPERIMENT – 22
22. Write a 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.
Source code:
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_dat
a)
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])

Sample output:
EXPERIMENT – 23
23. Write a R program to create a list containing a vector,
a matrix and a list and remove the second element.
Source code:
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_dat
a)
print("Remove the second element of
the list:") list_data[2] = NULL
print("New
list:")
print(list_data
)
Sample Output:

EXPERIMENT – 24
24. Write a R program to select second element of a
given nested list.
Source code:
x = list(list(0,2), list(3,4),
list(5,6)) print("Original
nested list:")
print(x)
e = lapply(x, '[[', 2)
print("Second element of the
nested list:") print(e)
Sample output:
EXPERIMENT – 25
25. Write a R program to merge two given lists into one list.
Source code:
n1 = list(1,2,3)
c1 = list("Red", "Green",
"Black") print("Original
lists:")
print(n
1)
print(c
1)
print("Merge the said
lists:") mlist = c(n1, c1)
print("New merged
list:") print(mlist)
Sample output:
EXPERIMENT – 26
26. Write a R program to create a list named s containing
sequence of 15 capital letters, starting from ‘E’.
Source code:
l = LETTERS[match("E", LETTERS):(match("E", LETTERS)
+15)]
print("Content of the list:")
print("Sequence of 15 capital letters, starting
from ‘E’-") print(l)
Sample output:

EXPERIMENT – 27
27. Write a R program to assign new names "a", "b" and
"c" to the elements of a given list.
Source code:
list1 = list(g1 = 1:10, g2 = "R Programming",
g3 = "HTML") print("Original list:")
print(list1)
names(list1) = c("one", "two", "three")
print("Assign new names 'one', 'two' and 'three' to the
elements of the said list")
print(list1)
Sample
output:

EXPERIMENT – 28
28. Write a R program to find the levels of factor of a given
vector.
Source code:
v = c(1, 2, 3, 3, 4, NA, 3, 2, 4, 5, NA, 5)
print("Original
vector:") print(v)
print("Levels of factor of the said
vector:") print(levels(factor(v)))
Sample output:

EXPERIMENT – 29
29. Write a R program to create an ordered factor
from data consisting of the names of months.
Source code:
mons_v = c("March","April","January","November","January",
"September","October","September","November","August","Fe
bruary",
"January","November","November","February","May","August",
"February",
"July","December","August","August","September","November",
"Septembe r",
"February","April")
print("Original
vector:")
print(mons_v)
f = factor(mons_v)
print("Ordered factors of the said
vector:") print(f)
print(table(f))
Sample Output:

EXPERIMENT – 30
30. Write a R program to concatenate two given factor in
a single factor.
Source code:
f1 <- factor(sample(LETTERS, size=6,
replace=TRUE)) f2 <-
factor(sample(LETTERS, size=6,
replace=TRUE)) print("Original factors:")
print(f
1)
print(f
2)
f = factor(c(levels(f1)[f1], levels(f2)
[f2])) print("After concatenate
factor becomes:") print(f)
Sample output:

********************

You might also like