Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

# Task 1: Consider a class of 5 students,

# A. Create 3 vectors which contians marks scored by the students in the


following:
# Maths(maths)- (70,75,80,85,90),
# Science(science) - (71,76,81,86,91),
# English(english)- (73,78,83,88,93).

maths <- c(70,75,80,85,90)


science <- c(71,76,81,86,91)
english <- c(73,78,83,88,93)

# B. Create a list using these 3 Vectors.


student_list <- list(maths, science, english)
# C. Calculate the class average in each of these subjects by using lapply()
and print it.
ans1 <- lapply(student_list, mean)
print(ans1)

# Task 2: Consider a class of 4 students:


# A. Create 4 vector which contain the marks of 4 students in 3 subjects -
Maths, Science, and English.
# The values are s1- (70,80,90), s2- (71,81,91), s3 <- c(72,82,92) and s4 <-
c(73,83,93).
s1 <- c(70,80,90)
s2 <- c(71,81,91)
s3 <- c(72,82,92)
s4 <- c(73,83,93)

# B. Create a list using the 4 vector.


student_list2 = list(s1,s2,s3,s4)
# C. Calculate the total score(mean) for each of the 4 students using the
function sapply() and print it.
total_score <- sapply(student_list2 , mean)
print(total_score)

# Task 3: Perform the following tasks:


# A. Create a 10X5 Matrix using the numbers from 1 to 50. (Arrange 1 to 50
not in row-wise order.)
# Hint: GIve byrow =FALSE

mtrix <- matrix(c(1:50), nrow = 10, ncol = 5, byrow = FALSE)


# print(mtrix)

# B. Find the Average of each column of the matrix by using the apply()
function and print it.
avg_cl <- apply(mtrix, 2, mean)
print(avg_cl)

# Task 4: For this excerise, let us the the R built-in Dataset mtcars.
# A. Using thr tapply() function, find the average 'mpg' for the various
transmission types(am) and number of cylinder(cyl) in a car and print it.
avg_mpg <- tapply( mtcars$mpg, list( mtcars$cyl ,mtcars$am) , mean )
print(avg_mpg)

# Task 5
# A. Create a vector x with the elements from 1 to 5,
# B. Create another vector y with elements from 6 to 10,

c <- c(1:5)
p <- c(6:10)

# C.Use mapply() to multiply the vector x and y, such that the first element of
x is multiplied with the first element of y and print it.
mult_one <- function(Data1,Data2)
{
Data1*Data2
}
cc <- mapply(mult_one,c, p )
print(cc)
}

loops()

You might also like