0% found this document useful (0 votes)
6 views

CS605 DA

lab manual
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

CS605 DA

lab manual
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

INTRODUCTION: - R PROGRAMMING LAB

COURSE OBJECTIVES:
• To learn statistical programming, computation, graphics, and modeling,
• To learn Writing functions and use R in an efficient way,
• To learn about basic types of statistical models
COURSE OUTCOMES:
At the end of this course, students will be able to:
• Access online resources for R and import new function packages into the R
workspace
• Import, review, manipulate and summarize data-sets in R
• Explore data-sets to create testable hypotheses and identify appropriate statistical tests
• Perform appropriate statistical tests using R
• Create and edit visualizations with R

INTRODUCTION: - MATLAB PROGRAMMING LAB

COURSE OBJECTIVES:
• To learn statistical programming, computation, graphics, and modeling,
• To learn Writing functions and use Matlab in an efficient way,
• To learn about basic types of statistical models

COURSE OUTCOMES:
At the end of this course, students will be able to:
• Access online resources for Matlab and import new function packages into the Matlab
workspace
• Import, review, manipulate and summarize data-sets in Matlab
• Explore data-sets to create testable hypotheses and identify appropriate statistical tests
• Perform appropriate statistical tests using Matlab
• Create and edit visualizations with Matlab
INTRODUCTION: - PYTHON PROGRAMMING LAB

COURSE OBJECTIVES:
• To learn statistical programming, computation, graphics, and modeling,
• To learn Writing functions and use Python in an efficient way,
• To learn about basic types of statistical models

COURSE OUTCOMES:
At the end of this course, students will be able to:
• Access online resources for Python and import new function packages into the Python
workspace
• Import, review, manipulate and summarize data-sets in Python
• Explore data-sets to create testable hypotheses and identify appropriate statistical tests
• Perform appropriate statistical tests using Python
• Create and edit visualizations with Python
List of Experiment
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 subjects marks.
5) Write a R program to get the unique elements of a given string and unique numbers of
vector.
6) Write a Matlab 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 Matlab 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 Matlab 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.
9) Write a Matlab program to create a two-dimensional 5x3 array of sequence of even integers greater
than 50.
10) Write a Matlab 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 Python program to create an empty data frame.
12) Write a Python program to create a data frame from four given vectors.
13) Write a Python 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 Python program to save the information of a data frame in a file and display the
information of the file.
15) Write a Python program to create a matrix from a list of given vectors.
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="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)
Sample Output:
Input your name:
Input your age:
"My name is and I am years old."
"R version 3.4.4 (2018-03-15)"
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(ls.str())
Sample Output:
"n1" "n2" "name" "nums"
"Details of the objects in memory:"
n1 : num 10
n2 : num 0.5
name : chr "Python"
nums : num [1:6] 10 20 30 40 50 60
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:
print ("Sequence of numbers from 20 to 50:")
print(seq(20,50))
print ("Mean of numbers from 20 to 60:")
print(mean(20:60))
print("Sum of numbers from 51 to 91:")
print(sum(51:91))
Sample Output:
"Sequence of numbers from 20 to 50:"
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
"Mean of numbers from 20 to 60:"
40
"Sum of numbers from 51 to 91:"
2911
4. Write a R program to create a simple bar plot of five subjects marks.
Source Code:
marks = c(70, 95, 80, 74)
barplot(marks,
main = "Comparing marks of 5 subjects",
xlab = "Marks",
ylab = "Subject",
names.arg = c("English", "Science", "Math.", "Hist."),
col = "darkred",
horiz = FALSE)
Sample Output:
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("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))
Sample Output:
"Original vector(string)"
"The quick brown fox jumps over the lazy dog."
"Unique elements of the said vector:"
"the quick brown fox jumps over the lazy dog."
"Original vector(number)"
12234456
"Unique elements of the said vector:"
123456
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=[1 ,10, 19;2, 11, 20 ;3, 12, 21]
A=
1 10 19
2 11 20
3 12 21
>> B=[4 13, 22;5,14, 23; 6, 15 ,24]
B=
4 13 22
5 14 23
6 15 24
>> C=[7 16, 25; 8 17,26; 9 18, 27]
C=
7 16 25
8 17 26
9 18 27
>> D=[1 ,4,7,10,13,16,19,22,25;2,5,8,11,14,17,20,23,26;3,6,9,12,15,18,21,24,27]
D=
1 4 7 10 13 16 19 22 25
2 5 8 11 14 17 20 23 26
3 6 9 12 15 18 21 24 27
%and then Result=Reshape(D,3,3,[])
Result=reshape(D,3,3,[])
Result(:,:,1) =
1 4 7
2 5 8
3 6 9
Result(:,:,2) =
10 13 16
11 14 17
12 15 18
Result(:,:,3) =
19 22 25
20 23 26
21 24 27
7) Write a 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:
"5 × 4 matrix:"
[,1] [,2] [,3] [,4]
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
"3 × 3 matrix with labels, filled by rows:”
8) Write a 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:-
array1 <- matrix(1:5, nrow = 1)
array2 <- matrix(6:10, nrow = 1)
array3 <- matrix(11:15, nrow = 1)
print("Array 1:")
print(array1)
print("Array 2:")
print(array2)
print("Array 3:")
print(array3)
# Combine arrays along row axis
combined_array <- rbind(array1, array2, array3)

# Print combined array


print("Combined Array:")
print(combined_array)
9) Write a Matlab program to create a two-dimensional 5x3 array of sequence of even integers
greater than 50.
Source Code:
# specifying where to start the
# even integers from
start_pos <- 20
# specifying dimensions of matrix
nrow <- 3
ncol <- 2
# calculating starting position
end_pos = (nrow * ncol * 2) + start_pos
# creating empty vector
vec <- c()
for (i in start_pos : end_pos-1){
# check if element is divisible by 2
if(!(i%%2)){
# append element to vector
vec <- c(vec, i )
}
}
mat <- matrix(vec , nrow = 3)
print ("Sequence of even integers in 2-D array")
print (mat)
Sample Output:
[1] "Sequence of even integers in 2-D array"
[,1] [,2]
[1,] 20 26
[2,] 22 28
[3,] 24 30
10) Write a Matlab 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:

LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'};
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(LastName,Age,Smoker,Height,Weight,BloodPressure)

Sample Output:
T=5×6 table
LastName Age Smoker Height Weight BloodPressure
___________ ___ ______ ______ ______ _____________

{'Sanchez'} 38 true 71 176 124 93


{'Johnson'} 43 false 69 163 109 77
{'Li' } 38 true 64 131 125 83
{'Diaz' } 40 false 67 133 117 75
{'Brown' } 49 true 64 119 122 80
11) Write a Python program to create an empty data frame.

Source Code:
df = data.frame(Ints=integer(),
Doubles=double(),
Characters=character(),
Logicals=logical(),
Factors=factor(),
stringsAsFactors=FALSE)
print("Structure of the empty dataframe:")
print(str(df))
Sample Output:

"Structure of the empty dataframe:"


'data.frame': 0 obs. of 5 variables:
$ Ints : int
$ Doubles : num
$ Characters: chr
$ Logicals : logi
$ Factors : Factor w/ 0 levels:
NULL
12) Write a Python 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(attempts)
print(qualify)
df = data.frame(name, score, attempts, qualify)
print(df)
Sample Output:
"Original data frame:"
"Anastasia" "Dima" "Katherine" "James" "Emily" "Michael"
"Matthew" "Laura" "Kevin" "Jonas"
12.5 9.0 16.5 12.0 9.0 20.0 14.5 13.5 8.0 19.0
1323231121
"yes" "no" "yes" "no" "no" "yes" "yes" "no" "no" "yes"
name score attempts qualify
1 Anastasia 12.5 1 yes
2 Dima 9.0 3 no
3 Katherine 16.5 2 yes
4 James 12.0 3 no
5 Emily 9.0 2 no
6 Michael 20.0 3 yes
7 Matthew 14.5 1 yes
8 Laura 13.5 1 no
9 Kevin 8.0 2 no
10 Jonas 19.0 1 yes
13) Write a Python 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 = 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))
Sample Output:
"Original data frame:"
ab
1 10 10
2 20 30
3 10 10
4 10 20
5 40 0
6 50 50
7 20 30
8 30 30
"Duplicate elements of the said data frame:"
FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
"Unique rows of the said data frame:"
ab
1 10 10
2 20 30
4 10 20
5 40 0
6 50 50
8 30 30
14) Write a Python program to save the information of a data frame in a file and display the
information of the file.
Source Code:
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")
Sample Output:
"Original dataframe:"
name score attempts qualify
1 Anastasia 12.5 1 yes
2 Dima 9.0 3 no
3 Katherine 16.5 2 yes
4 James 12.0 3 no
5 Emily 9.0 2 no
6 Michael 20.0 3 yes
7 Matthew 14.5 1 yes
8 Laura 13.5 1 no
9 Kevin 8.0 2 no
10 Jonas 19.0 1 yes
size isdir mode mtime ctime
data.rda 344 FALSE 644 2018-10-25 12:06:09 2018-10-25 12:06:09
atime uid gid uname grname
data.rda 2018-10-25 12:06:09 1000 1000 trinket trinket
15) Write a Python 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 = do.call(rbind, l)
print("New Matrix:")
print(result)
Sample Output:
"List of vectors:"
[[1]]
11234
[[2]]
21234
[[3]]
31234
[[4]]
41234
[[5]]
51234
"New Matrix:"
[,1] [,2] [,3] [,4] [,5]
11234
21234
31234
41234
5123

You might also like