Sie sind auf Seite 1von 4

#Assignment 2

#Part1: Creating a vector, matrix, list and data frame

#1.Create below vectors with 5 elements in each vector:


#Student.id
#Student.name
#Marks
#Qualification
#Print the count of elements in each vector.

Student.id = 101:105
Student.name = c('Abhay','Disha','Devika','Parv','Sonal')
Marks = seq(50,60,2.5)
Qualification = c('UG','PG','PG','UG','UG')

length(Student.id)
length(Student.name)
length(Marks)
length(Qualification)

#2.Create a matrix using below details:


#Data 1:12
#Number of columns 4
#Row names r1, r2, r3
#Column names c1, c2, c3, c4

#Data to be entered by row


#Print the dimension of the matrix
#Print row and column names of the matrix

m1 = matrix(1:12, ncol=4, byrow = TRUE)


m1
rownames(m1) = c('r1','r2','r3')
colnames(m1) = c('c1','c2','c3','c4')
m1

rownames(m1)
colnames(m1)

#3. Create 3 element list by using below details:


#Element 1: Matrix created in Part1.2.
#Element 2: Logical value
#Element 3: Character vector Student.name created in Part1.1.

l1 = list(m1,TRUE,Student.name)
l1

#4.Create a data frame using below details:


#Data = Vectors created in Part1. 1.
#Character vectors should retain their type and not to be converted into factors.
#Print the dimension of the data frame
#Print row and column names of the data frame
df1 = data.frame(Student.id
,Student.name
,Marks
,Qualification
,stringsAsFactors = FALSE)
df1
dim(df1)

rownames(df1)
colnames(df1)

#----------------------------------------------------------
#Part2 : Loops and Functions

#1. Loop through the vector of Student.name created in Part1. 1 using for loop
and print names.

for (i in Student.name)
{
print(i)
}

#2. Create a vector using seq function starting from 201 to 210.
vec = seq(201,210)

#3. Create a function to return mean of the vector


# =>Input to the function should be a vector.
# =>Use default value of 0 for the parameter.
# =>Call the function created by passing the vector created in Part 2.2.

c_mean = function(x=0)
{
total = 0

for (i in x)
{
total = total + i
}

mean_val = total/length(x)

return(mean_val)
}

#Standard function to calculate mean


#This function is to be used for validating the answer returned by user defined
mean function
mean(vec)

#User defined function to calculate mean


c_mean(vec)
c_mean()
#4. Create a function to return the standard deviation of the vector
# =>Input to the function should be a vector.
# =>Use default value of 0 for the parameter.
# =>Use the mean function created in above question (Part 2.3.) to calculate
standard deviation.
# =>Call the function created and pass the vector created in Part 2.2.

c_sd = function(x = 0)
{
mean_val = c_mean(x)
sq_sum = 0

for (i in x)
{
sq_sum = sq_sum + (i - mean_val)^2
}

var_val = sq_sum/(length(x)-1)
sd_val = var_val^(1/2)

return(sd_val)
}

#Standard function to calculate standard deviation


sd(vec)

#User defined function to calculate standard deviation


c_sd(vec)
c_sd()

#5. Create a function to return the variance of the vector.


# =>Input to the function should be a vector.
# =>Use default value of 0 for the parameter.
# =>Use the mean function created in above question (Part 2.3.) to calculate
variance.
# =>Call the function created and pass the vector created in Part 2.2.

c_var = function(x = 0)
{
mean_val = c_mean(x)
sq_sum = 0

for (i in x)
{
sq_sum = sq_sum + (i - mean_val)^2
}

var_val = sq_sum/(length(x)-1)

return(var_val)
}

#Standard function to calculate variance


var(vec)
#User defined function to calculate variance
c_var(vec)
c_var()

Das könnte Ihnen auch gefallen