mapply() works with multivariate arrays, and applys a function to a set of vector or list arguments. mapply() also simplifies the output. Structure of the mapply() function: mapply(FUN, …, MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE) Answers to the exercises are available here. Exercise 1 Beginning level Required dataframe: PersonnelData <- data.frame(Representative=c(1:4), Sales=c(95,110,115,90), […]

## Multivariate Apply Solutions

Below are the solutions to these exercises on Multivariate Apply. #################### # # # Exercise 1 # # # #################### PersonnelData <- data.frame(Representative=c(1:4), Sales=c(95,110,115,90), Territory=c(1:4)) mapply(class, PersonnelData) ## Representative Sales Territory ## “integer” “numeric” “integer” #################### # # # Exercise 2 # # # #################### mapply(print, PersonnelData) ## [1] 1 2 3 4 ## [1] […]

## Optimize Data Exploration With Sapply() – Exercises

The apply() functions in R are a utilization of the Split-Apply-Combine strategy for Data Analysis, and are a faster alternative to writing loops. The sapply() function applies a function to individual values of a dataframe, and simplifies the output. Structure of the sapply() function: sapply(data, function, …) The dataframe used for these exercises: dataset1 <- […]

## Optimize Data Exploration With Sapply() – Solutions

Below are the solutions to these exercises on Optimize Data Exploration With Sapply(). #################### # # # Exercise 1 # # # #################### dataset1 <- data.frame(observationA = 16:8, observationB = c(20:19, 6:12)) sapply(dataset1, length) ## observationA observationB ## 9 9 #################### # # # Exercise 2 # # # #################### sapply(dataset1, sum) ## observationA observationB […]

## Applying Functions To Lists Exercises

The lapply() function applies a function to individual values of a list, and is a faster alternative to writing loops. Structure of the lapply() function: lapply(LIST, FUNCTION, …) The list variable used for these exercises: list1 <- list(observationA = c(1:5, 7:3), observationB=matrix(1:6, nrow=2)) Answers to the exercises are available here. Exercise 1 Using lapply(), find […]

## Applying Functions To Lists Solutions

Below are the solutions to these exercises on Applying Functions To Lists. #################### # # # Exercise 1 # # # #################### list1 <- list(observationA = c(1:5, 7:3), observationB=matrix(1:6, nrow=2)) lapply(list1, length) ## $observationA ## [1] 10 ## ## $observationB ## [1] 6 #################### # # # Exercise 2 # # # #################### lapply(list1, sum) […]

## Efficient Processing With Apply() Exercises

The apply() function is an alternative to writing loops, via applying a function to columns, rows, or individual values of an array or matrix. The structure of the apply() function is: apply(X, MARGIN, FUN, …) The matrix variable used for the exercises is: dataset1 <- cbind(observationA = 16:8, observationB = c(20:19, 6:12)) Answers to the […]

## Efficient Processing With Apply() Solutions

Below are the solutions to these exercises on Efficient Processing With Apply(). #################### # # # Exercise 1 # # # #################### dataset1 <- cbind(observationA = 16:8, observationB = c(20:19, 6:12)) apply(dataset1, 1, mean) ## [1] 18 17 10 10 10 10 10 10 10 #################### # # # Exercise 2 # # # #################### […]

## Reshape 2 Exercises

The Reshape 2 package is based on differentiating between identification variables, and measurement variables. The functions of the Reshape 2 package then “melt” datasets from wide to long format, and “cast” datasets from long to wide format. Required package: library(reshape2) Answers to the exercises are available here. Exercise 1 Set a variable called “moltenMtcars“, by […]

## Reshape 2 Solutions

Below are the solutions to these exercises on The Reshape 2 Package. #################### # # # Exercise 1 # # # #################### moltenMtcars <- melt(mtcars, id.vars = c("cyl", "gear")) #################### # # # Exercise 2 # # # #################### CarSurvey <- dcast(moltenMtcars, cyl + gear ~ variable, mean) #################### # # # Exercise 3 # […]