# Practice Exercises on Loop Functions.rtf - loops <-...

• 18

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 1 - 10 out of 18 pages.

loops <- function(){#Write your code heremaths<- c(70,75,80,85,90)science <- c(71,76,81,86,91)english <- c(73,78,83,88,93)class_average <- list(maths,science, english)print(lapply(class_average, mean))#Exercise 2s1 <- c(70,80,90)
s2 <- c(71,81,91)s3 <- c(72,82,92)s4 <- c(73,83,93)total_score <- list(s1,s2,s3,s4)print(sapply(total_score,mean))# Exercise 3average_column <-matrix(1:50, nrow=10, ncol=5)print(apply(average_column,2,mean))# Exercise 4print(tapply(mtcars\$mpg, list(mtcars\$cyl, mtcars\$am), mean))# Exercise 5x <- c(1:5)y <- c(6:10)print(mapply(function(x, y) x*y, x, y))
}loops()PRACTICE EXERCISE ON DATA FRAMES
data_handle_1 <- function(){#Write your code herename <- c("A","B","C","D","E")age <- c(10,11,12,13,14)department <- c("AA","BB","CC","DD","EE")empdetails <- data.frame(name,age,department)print(empdetails)print(str(empdetails))print(str(mtcars))print(summary(mtcars))print(mtcars[, c(1, 2, 10)])
print(mtcars[order(mtcars\$mpg),])cars <- mtcars[which(mtcars\$mpg>30),]print(cars)print(cars[order(-cars\$mpg),])}data_handle_1()Create a Data Table using the R built-in dataset mtcarsFind the average-weight(wt) & average horse-power(hp) of all cars with a mileage above 15 mpg,grouped by number of cylinders(cyl) and carburettors(carb).s<-data.table(mtcars)s[mpg>15,.(mean(wt),mean(hp)),by=.(cyl,carb)]If is for Fresco you need to add the librarylibrary(data.table)s<-data.table(mtcars)s[mpg>15,.(mean(wt),mean(hp)),by=.(cyl,carb)]data_handle_1 <- function(){
#Write your code herename <- c("A","B","C","D","E")age <- c(10,11,12,13,14)department <- c("AA","BB","CC","DD","EE")empdetails <- data.frame(name,age,department)#print(empdetails,row.names=FALSE)A <- str(empdetails)print(A)str_cars <- str(mtcars)print(str_cars)summary_cars <- summary(mtcars)print(summary_cars)print(mtcars[, c(1, 2, 10)],row.names=FALSE)order_mtcars <- mtcars[order(mtcars\$mpg),]print(order_mtcars)print(mtcars[which(mtcars\$mpg>30),] )more_than_30_mpg <- mtcars[which(mtcars\$mpg>30),]

Course Hero member to access this document

Course Hero member to access this document

End of preview. Want to read all 18 pages?

Course Hero member to access this document

Term
Fall
Professor
NoProfessor