<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/multivariate-apply-exercises/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2017/01/17/multivariate-apply-exercises/?utm_source=rss&utm_medium=rss&utm_campaign=multivariate-apply-exercises"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2017/01/maple-leaf-984945__340.jpg?x61433"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-3945" src="https://i1.wp.com/r-exercises.com/wp-content/uploads/2017/01/maple-leaf-984945__340-150x150.jpg?resize=150%2C150" data-recalc-dims="1"></a><code>mapply()</code> works with multivariate arrays, and applys a function to a set of vector or list arguments. <code>mapply()</code> also simplifies the output.</p>
<p>Structure of the <code>mapply()</code> function:<br>
<code>mapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE)</code></p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/01/17/multivariate-apply-solutions/">here</a>.</p>
<p><strong>Exercise 1</strong><br>
Beginning level</p>
<p>Required dataframe:<br>
<code>PersonnelData &lt;- data.frame(Representative=c(1:4),<br>
Sales=c(95,110,115,90), Territory=c(1:4))</code></p>
<p>Using <code>mapply()</code>, find the classes of <code>PersonnelData</code>‘s columns.</p>
<p><strong>Exercise 2</strong><br>
Beginning level</p>
<p>Print “<code>PersonnelData</code>” with the <code>mapply()</code> function.</p>
<p><strong>Exercise 3</strong><br>
Beginning level</p>
<p>Use <code>mapply()</code> to inspect “<code>PersonnelData</code>” for numeric values.</p>
<p><strong>Exercise 4</strong><br>
Intermediate level</p>
<p>Use <code>mapply()</code> to sum the vectors “<code>5:10</code>” and “<code>20:25</code>“.</p>
<p><strong>Exercise 5</strong><br>
Intermediate level</p>
<p>Use <code>mapply()</code> to paste the vector “<code>1:4</code>” and “<code>5:8</code>“, with the separator “<code>LETTERS[1:4]</code>“.</p>
<div>
<aside><img src="http://r-exercises.com/2017/01/17/multivariate-apply-exercises/?utm_source=rss&utm_medium=rss&utm_campaign=multivariate-apply-exercises"></aside>
<div><strong>Learn more</strong> about mapply, and the entire family of apply() functions in the online course <a target="_blank" rel="nofollow" href="http://r-exercises.com/product/learn-r-by-intensive-practice/">Learn R by Intensive Practice</a>. In this course you will learn how to:
<div data-more="Full details">
<div>
<ul>
<li>Do any sort of manipulation with datasets</li>
<li>Create and master the manipulation of vectors, lists, dataframes, and matrices</li>
<li>Confidently write apply() functions and design any logic within the apply function.</li>
<li>Melt, reshape, aggregate, and make pivot tables from dataframes</li>
<li>And much more</li>
</ul>
</div>
</div>
</div>
</div>
<p><strong>Exercise 6</strong><br>
Intermediate level</p>
<p>Use <code>mapply()</code> to paste “<code>PersonnelData$Representative</code>“, “<code>PersonnelData$Sales</code>“, and “<code>PersonnelData$Territory</code>“, with the<br>
“<code>MoreArgs=</code>” argument of “<code>list(sep=&quot;-&quot;)</code>“.</p>
<p><strong>Exercise 7</strong><br>
Advanced level</p>
<p>Required variable:<br>
<code>NewSales &lt;- data.frame(Representative=c(1:4), Sales=c(104, 97, 112, 94), Territory=c(1:4))</code></p>
<p>Sum the corresponding elements of <code>PersonnelData$Sales</code> and <code>NewSales$Sales</code>.</p>
<p><strong>Exercise 8</strong><br>
Advanced level</p>
<p>Required function:<br>
<code>merge.function &lt;- function(x,y){return(x+y)}</code></p>
<p>Use <code>merge.function</code> to combine the <code>Sales</code> totals from <code>PersonnelData</code> and <code>NewSales</code>.</p>
<p><strong>Exercise 9</strong><br>
Advanced level</p>
<p><code>mcmapply</code> is a parallelized version of <code>mapply</code>.</p>
<p>The structure of <code>mcmapply()</code> is:<br>
<code>mcmapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE, mc.preschedule = TRUE, mc.set.seed = TRUE, mc.silent = FALSE, mc.cores = getOption(&quot;mc.cores&quot;, 2L), mc.cleanup = TRUE)</code></p>
<p>Required library:<br>
<code>library(parallel)</code></p>
<p>Use <code>mcmapply()</code> to generate 5 lists of <code>1:5</code> random numbers.</p>
<p><strong>Exercise 10</strong><br>
Advanced level</p>
<p>Using <code>mcmapply()</code>, create a 10 by 10 matrix with 10 rows of the sequence <code>1:10</code>:</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-exercises.com/2017/01/17/multivariate-apply-exercises/?utm_source=rss&utm_medium=rss&utm_campaign=multivariate-apply-exercises"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/8pa9K4Ey3kc"></a></div>