Multivariate Apply Exercises

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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), Territory=c(1:4))

Using mapply(), find the classes of PersonnelData‘s columns.

Exercise 2
Beginning level

Print “PersonnelData” with the mapply() function.

Exercise 3
Beginning level

Use mapply() to inspect “PersonnelData” for numeric values.

Exercise 4
Intermediate level

Use mapply() to sum the vectors “5:10” and “20:25“.

Exercise 5
Intermediate level

Use mapply() to paste the vector “1:4” and “5:8“, with the separator “LETTERS[1:4]“.

Learn more about mapply, and the entire family of apply() functions in the online course Learn R by Intensive Practice. In this course you will learn how to:

  • Do any sort of manipulation with datasets
  • Create and master the manipulation of vectors, lists, dataframes, and matrices
  • Confidently write apply() functions and design any logic within the apply function.
  • Melt, reshape, aggregate, and make pivot tables from dataframes
  • And much more

Exercise 6
Intermediate level

Use mapply() to paste “PersonnelData$Representative“, “PersonnelData$Sales“, and “PersonnelData$Territory“, with the
MoreArgs=” argument of “list(sep="-")“.

Exercise 7
Advanced level

Required variable:
NewSales <- data.frame(Representative=c(1:4), Sales=c(104, 97, 112, 94), Territory=c(1:4))

Sum the corresponding elements of PersonnelData$Sales and NewSales$Sales.

Exercise 8
Advanced level

Required function:
merge.function <- function(x,y){return(x+y)}

Use merge.function to combine the Sales totals from PersonnelData and NewSales.

Exercise 9
Advanced level

mcmapply is a parallelized version of mapply.

The structure of mcmapply() is:
mcmapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE, mc.preschedule = TRUE, mc.set.seed = TRUE, mc.silent = FALSE, mc.cores = getOption("mc.cores", 2L), mc.cleanup = TRUE)

Required library:
library(parallel)

Use mcmapply() to generate 5 lists of 1:5 random numbers.

Exercise 10
Advanced level

Using mcmapply(), create a 10 by 10 matrix with 10 rows of the sequence 1:10:

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)