Assignment 1
Abhay Joseph C J
2025-07-22
# Q1. Vector Creation
# a. Create a numeric vector x that contains the values: 3, 7 , 9 , 12 , and
15
###############################################
#ans
x = c(3,7,9,12,15)
x
## [1] 3 7 9 12 15
#2. Vector Arithmetic
#Let x = c(1, 3, 5) and y = c(2, 4, 6).
#a. Compute x + y
#b. Compute x - y
#c. Compute x * y (element-wise multiplication)
#d. Compute x / y (element-wise division)
################################################
#ans
#(a)
x = c(1, 3, 5)
x
## [1] 1 3 5
y = c(2, 4, 6)
p = x+y
p
## [1] 3 7 11
#(b)
q= x-y
q
## [1] -1 -1 -1
#(C)
r= x*y
r
## [1] 2 12 30
#(d)
s = x/y
s
## [1] 0.5000000 0.7500000 0.8333333
#Q3. Linear Combination of Vectors
#Let u = c(2, 4, 6) and v = c(1, 3, 5).
#a. Compute the linear combination: 3u – 2v
#######################################
#Ans
u = c(2,4,6)
u
## [1] 2 4 6
v = c(1,3,5)
v
## [1] 1 3 5
a =3*u
a
## [1] 6 12 18
b=2*v
b
## [1] 2 6 10
c = a-b
c
## [1] 4 6 8
#Q4. Dot Product (Inner Product)
#Let a = c(4, 1, -2) and b = c(2, 5, 3).
#a. Find the dot product of a and b.
############################
#Ans
a=c(4, 1, -2)
b=c(2, 5, 3)
dot_prod = sum(a * b)
dot_prod
## [1] 7
# Q5. Magnitude (Euclidean Norm)
#Let w = c(6, 8).
#a. Compute the Euclidean norm (length) of the vector w.
###################################################
#Ans
w=c(6,8)
magnitude=sqrt(sum(w^2))
magnitude
## [1] 10
# Q6. 6. Vector Indexing
#Let z = c(10, 20, 30, 40, 50).
#a. Extract the 2nd and 4th elements of z
#b. Replace the 3rd element with 99
#c. Extract all elements of z that are greater than 25
#########################################
#(a)
z = c(10, 20, 30, 40, 50)
y=z[c(2, 4)]
y
## [1] 20 40
#(b)
z[3]= 99
z
## [1] 10 20 99 40 50
#(c)
greater_than_25=z[z>25]
greater_than_25
## [1] 99 40 50
# Q7. Vector Summary Statistics
#Let scores = c(72, 88, 91, 67, 85, 77).
#a. Find the maximum and minimum score
#b. Find the mean and median
#c. Find the standard deviation
#d. Find the range
###########################
#Ans
#(a)
c(72, 88, 91, 67, 85, 77)
## [1] 72 88 91 67 85 77
x=c(72, 88, 91, 67, 85, 77)
max_score= max(x)
max(x)
## [1] 91
min_score = min(x)
min(x)
## [1] 67
# b)
mean_value=mean(x)
mean(x)
## [1] 80
median_value=median(x)
median(x)
## [1] 81
# c)
std_dev=sd(x)
sd(x)
## [1] 9.507891
# d)
range_values=range(x)
range(x)
## [1] 67 91
range_diff= max(x) - min(x)
max(x) - min(x)
## [1] 24
# Q8 Sorting and Ranking
#Let x = c(45, 22, 89, 33, 65).
#a. Sort the values in ascending order
#b. Sort the values in descending order
#c. Find the rank of each value in x
################################
#(a)
x = c(45, 22, 89, 33, 65)
asc_x = sort(x)
asc_x
## [1] 22 33 45 65 89
#(b)
x= c(45, 22, 89, 33, 65)
desc_x = sort(x ,decreasing = TRUE)
desc_x
## [1] 89 65 45 33 22
#(C)
x= c(45, 22, 89, 33, 65)
rank(x)
## [1] 3 1 5 2 4
#Q 9. Repetition and Sequences
#a. Create a vector that repeats the number 7 ten times
#b. Create a sequence from 1 to 100 in steps of 5
#c. Repeat the pattern: 1, 2, 3 – three times
##############################################
# Ans (a)
x=vec=rep(7, 10)
x
## [1] 7 7 7 7 7 7 7 7 7 7
# (b)
y=seq(1,100,5)
y
## [1] 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96
#(c)
z= rep(c(1, 2, 3), times = 3)
z
## [1] 1 2 3 1 2 3 1 2 3
# Q10. Interpretation-Based (Conceptual)
#Let x = c(5, 10, 15) and y=c(2, 4, 6).
#a. without using R, explain what x * y might represent in a real-world
multivariate context (e.g., income x hours worked).
#b.Explain what x + y might represent in a context such as total cost from
two sources.
#####################################
#Ans
#(a)
x = c(5, 10, 15)
x
## [1] 5 10 15
y=c(2, 4, 6)
y
## [1] 2 4 6
# x* y — Example: Apples sold × Price per apple
#Let x = c(5, 10, 15) = number of apples sold on 3 different days
#Let y = c(2, 4, 6) = price per apple on those days
# Then x * y = total money earned each day:
#Day 1: 5 apples × Rs2 = Rs10
#Day 2: 10 apples × Rs4 = Rs40
#Day 3: 15 apples × Rs6 = Rs90
#(b).
#x + y = Example: Cost of a toy + Cost of wrapping
#x =c(5, 10, 15) = cost of the toy
x
## [1] 5 10 15
#y = c(2, 4, 6) = cost of wrapping paper
y
## [1] 2 4 6
#Then x + y = total gift cost:
#Gift 1: Rs5 + Rs2 = Rs7
#Gift 2: Rs10 + Rs4 = Rs14
#Gift 3: Rs15 + Rs6 = Rs21