Professional Documents
Culture Documents
Data Handling in R - Loop functions
Data Handling in R - Loop functions
# 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()