R Programming Lab Manual
R Programming Lab Manual
0|Page
R20@II CSE |JNTU KAKINADA SYLLABUS
LIST OF PROGRAMS
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.
2) Write a R program to get the details of the objects in memory.
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.
4) Write a R program to create a simple bar plot of five subject marks.
5) Write a R program to get the unique elements of a given string and unique numbers of
vector.
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.
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.
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 the first row of the third array.
9) Write a R program to create a two-dimensional 5x3 array of sequence of even integers
greater than 50.
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.
11) Write a R program to create an empty data frame.
12) Write a R program to create a data frame from four given vectors.
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.
14) Write a R program to save the information of a data frame in a file and display the
information of the file.
15) Write a R program to create a matrix from a list of given vectors.
16) Write a R program to concatenate two given matrices of the same column but different
rows.
17) Write a R program to find row and column index of maximum and minimum value in a
given matrix.
18) Write a R program to append value to a given empty vector.
19) Write a R program to multiply two vectors of integers type and length 3.
20) Write a R program to find Sum, Mean and Product of a Vector, ignore element like NA
or NaN.
21) Write a R program to list containing a vector, a matrix and a list and give names to the
elements in the list.
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.
23) Write a R program to create a list containing a vector, a matrix and a list and remove
the
second element.
1
24) Write a R program to select the second element of a given nested list.
25) Write a R program to merge two given lists into one list.
26) Write a R program to create a list named s containing sequence of 15 capital letters,
starting from ‘E’.
27) Write a R program to assign new names "a", "b" and "c" to the elements of a given list.
28) Write a R program to find the levels of the factor of a given vector.
29) Write a R program to create an ordered factor from data consisting of the names of
months.
30) Write a R program to concatenate two given factors into a single factor.
2
SOLUTIONS TO EXERCISE QUESTIONS
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.
Answer:
name = readline(prompt="Input your name: ")
age = readline(prompt="Input your age: ")
print(paste("My name is",name, "and I am",age ,"years old."))
print(R.version.string)
3
horiz = FALSE)
5) Write a R program to get the unique elements of a given string and unique
numbers of Vectors.
Answer:
str1 = "The quick brown fox jumps over the lazy dog."
print("Original vector(string)")
print(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("Unique elements of the said vector:")
print(unique(nums))
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.
Answer:
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)
4
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.
Answer:
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 the first row of the third array.
Answer:
5
print(a)
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.
Answer:
df = data.frame(Ints=integer(),
Doubles=double(),
Characters=character(),
Logicals=logical(),
Factors=factor(),
stringsAsFactors=FALSE)
print("Structure of the empty dataframe:")
print(str(df))
12) Write a R program to create a data frame from four given vectors.
Answer:
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(attempts)
print(qualify)
df = data.frame(name, score, attempts, qualify)
print(df)
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.
6
Answer:
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 = data.frame(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))
14) Write a R program to save the information of a data frame in a file and display the
information of the file.
Answer:
exam_data = data.frame(
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("data.rda")
file.info("data.rda")
l = list()
for (i in 1:5) l[[i]] <- c(i, 1:4)
print("List of vectors:")
print(l)
result = do.call(rbind, l)
print("New Matrix:")
print(result)
16) Write a R program to concatenate two given matrices of the same column but
different rows.
Answer:
7
# R program for combining two matrices
# column-wise
# Original Matrices
print(B)
print(C)
# Combining matrices
print (cbind(B, C))
17) Write a R program to find the row and column index of maximum and minimum
value in a given matrix.
Answer:
m = matrix(c(1:16), nrow = 4, byrow = TRUE)
print("Original Matrix:")
print(m)
result = which(m == max(m), arr.ind=TRUE)
print("Row and column of maximum value of the said matrix:")
print(result)
result = which(m == min(m), arr.ind=TRUE)
print("Row and column of minimum value of the said matrix:")
print(result)
19) Write a R program to multiply two vectors of integers type and length 3.
Answer:
x = c(10, 20, 30)
y = c(20, 10, 40)
print("Original Vectors:")
8
print(x)
print(y)
print("Product of two Vectors:")
z=x*y
print(z)
20) Write a R program to find Sum, Mean and Product of a Vector, ignore elements
like NA or NaN.
Answer:
x = c(10, NULL, 20, 30, NA)
print("Sum:")
#ignore NA and NaN values
print(sum(x, na.rm=TRUE))
print("Mean:")
print(mean(x, na.rm=TRUE))
print("Product:")
print(prod(x, na.rm=TRUE))
21) Write a R program to list containing a vector, a matrix and a list and give names
to the elements in the list.
Answer:
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.
Answer:
9
print(list_data[1])
print('2nd element:')
print(list_data[2])
10
23) Write a R program to create a list containing a vector, a matrix and a list and
remove the second element.
Answer:
24) Write a R program to select the second element of a given nested list.
Answer:
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)
25) Write a R program to merge two given lists into one list.
Answer:
n1 = list(1,2,3)
c1 = list("Red", "Green", "Black")
print("Original lists:")
print(n1)
print(c1)
print("Merge the said lists:")
mlist = c(n1, c1)
print("New merged list:")
print(mlist)
11
26) Write a R program to create a list named s containing a sequence of 15 capital
letters, starting from ‘E’.
Answer:
27) Write a R program to assign new names "a", "b" and "c" to the elements of a
given list.
Answer:
28) Write a R program to find the levels of the factor of a given vector.
Answer:
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)))
29) Write a R program to create an ordered factor from data consisting of the names
of Months.
Answer:
mons_v = c("March","April","January","November","January",
"September","October","September","November","August","February",
"January","November","November","February","May","August","February",
"July","December","August","August","September","November","September",
"February","April")
print("Original vector:")
print(mons_v)
f = factor(mons_v)
print("Ordered factors of the said vector:")
12
print(f)
print(table(f))
30) Write a R program to concatenate two given factors into a single factor.
Answer:
13