<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/functions-exercises-vol-2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2016/12/05/functions-exercises-vol-2/"> 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="https://i1.wp.com/r-exercises.com/wp-content/uploads/2016/02/Clip_image002.gif?resize=273%2C226"><img alt="functions exercises" class="wp-image-311" src="https://i1.wp.com/r-exercises.com/wp-content/uploads/2016/02/Clip_image002.gif?resize=273%2C226" data-recalc-dims="1"></a></p>
<p>[For this exercise, first write down your answer, without using R. Then, check your answer using R.]</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2016/12/05/functions-vol-2-solutions/">here</a>.</p>
<p><strong>Exercise 1</strong></p>
<p>Create a function that given a data frame and a vector, will add a the vector (if the vector length match with the rows number of the data frame)<br>
as a new variable to the data frame. </p>
<p><strong>Exercise 2</strong></p>
<p>Consider a data frame df:<br>
<code><br>
Id=c(1:10)<br>
Age=c(14,12,15,10,23,21,41,56,78,12)<br>
Sex=c('F','M','M','F','M','F','M','M','F','M')<br>
Code=letters[1:10]<br>
df=data.frame(Id,Age,Sex,Code)<br>
</code></p>
<p>Create a function that, given a data frame and two indexes, exchanges two values ​​of the Code variable with each other.<br>
For example, if the index is 1 and 3, you assign:<br>
<code><br>
df[1,'Code']=df[3,'Code']<br>
df[3,'Code']=df[1,'Code']<br>
</code></p>
<p><strong>Exercise 3</strong></p>
<p>Consider two variables x,y and a data frame df:<br>
<code><br>
x,y integer
<p>A=c(1:10)<br>
B=seq(100,10,-10)<br>
H=seq(-200,-50,along.with=B)<br>
df=data.frame(A,B,H)<br>
</p>
<p>Create a function that given a data frame df calculate a new variable ‘SUM_x_y'(If x=2 and y=3, then the new variable will be ‘SUM_2_3’,<br>
if x=4 and y=10, then the new variable will be ‘SUM_4_10’),such that for each row ‘i’ is equal to:<br>
<code><br>
sum(x*df[1:i,1])+sum(y*df[1:i,2])<br>
</code></p>
<p><strong>Exercise 4</strong></p>
<p>Create a function that given a numeric vector, sort this in ascending order and duplicate it by two. </p>
<p><strong>Exercise 5</strong></p>
<p>Create a function that given a vector alpha numeric, keep only the numbers and apply the function created on exercise 4.<br>
For example, if the input is a vector <code>w=&quot;a&quot; &quot;v&quot; &quot;7&quot; &quot;4&quot; &quot;q&quot;</code> , the function will return <code>w=8 14</code>.</p>
<p><strong>Exercise 6</strong></p>
<p>Create a function that given a string<br>
<code><br>
ST='NAME: Maria /COUNTRY:uruguay /EMAIL: mariaUY@gmail.com'<br>
</code><br>
return a matrix<br>
<code><br>
	[,1]      [,2]<br>
[1,] &quot;NAME&quot;    &quot; Maria &quot;<br>
[2,] &quot;COUNTRY&quot; &quot;uruguay &quot;<br>
[3,] &quot;EMAIL&quot;   &quot; mariaUY@gmail.com&quot;<br>
</code></p>
<p><strong>Exercise 7</strong></p>
<p>Consider a vector:<br>
<code><br>
ST=c('NAME:Maria /COUNTRY:uruguay /EMAIL:mariaUY@gmail.com','NAME:Paul/COUNTRY:UK /EMAIL:PaulUK@gmail.com',<br>
     'NAME:Jhon /COUNTRY:USA /EMAIL:JhonUSA@gmail.com','NAME:Carlos /COUNTRY:Spain /EMAIL:CarlosSP@gmail.com')<br>
</code></p>
<p>Create a function that given a vector string ST return a matrix:<br>
<code><br>
    [,1]      [,2]                [,3]               [,4]                [,5]<br>
[1,] &quot;NAME&quot;    &quot;Maria &quot;            &quot;Paul&quot;             &quot;Jhon &quot;             &quot;Carlos &quot;<br>
[2,] &quot;COUNTRY&quot; &quot;uruguay &quot;          &quot;UK &quot;              &quot;USA &quot;              &quot;Spain &quot;<br>
[3,] &quot;EMAIL&quot;   &quot;mariaUY@gmail.com&quot; &quot;PaulUK@gmail.com&quot; &quot;JhonUSA@gmail.com&quot; &quot;CarlosSP@gmail.com&quot;<br>
</code></p>
<p><strong>Exercise 8</strong></p>
<p>Create a function that given a numeric vector X returns the digits 0 to 9 that are not in X. If <code>X=0 2 4 8 </code><br>
the function return <code>1 3 5 6 7 9</code></p>
<p><strong>Exercise 9</strong></p>
<p>Create a function that given two strings (one word each), check if one is an anagram of another.</p>
<p><strong>Exercise 10</strong><br>
Create a function that given one word, return the position of word’s letters on <code>letters</code> vector.<br>
For example, if the word is ‘abc’, the function will return 1 2 3.</p>
<p><strong>Want to practice functions a bit more? We have more exercise sets on this topic <a target="_blank" rel="nofollow" href="http://r-exercises.com/tag/function">here</a>.</strong></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/2016/12/05/functions-exercises-vol-2/"> 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/iFilyXGZcUs"></a></div></code>