<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/print-hello%e2%80%8b-is-not-enough-a-collection-of-hello-world-functions/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-posts.com/print-hello%E2%80%8B-is-not-enough-a-collection-of-hello-world-functions/"> R-posts.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img data-orig-file="https://i0.wp.com/r-posts.com/wp-content/uploads/2017/12/download.png?fit=299%2C168" data-orig-size="299,168" data-medium-file="https://i0.wp.com/r-posts.com/wp-content/uploads/2017/12/download.png?fit=299%2C168" data-attachment-id="395" data-image-meta="{" data-image-description="" src="https://i0.wp.com/r-posts.com/wp-content/uploads/2017/12/download.png?resize=299%2C168" data-permalink="http://r-posts.com/print-hello%e2%80%8b-is-not-enough-a-collection-of-hello-world-functions/download/" data-large-file="https://i0.wp.com/r-posts.com/wp-content/uploads/2017/12/download.png?fit=299%2C168" alt="" data-image-title="download" width="299" data-comments-opened="1" class="wp-image-395" data-recalc-dims="1" height="168"><br>
I guess I wrote my R “hello world!” function 7 or 8 years ago while approaching R for the first time. And it is too little to illustrate the basic syntax of a programming language for a working program to a wannabe R programmer. Thus, here follows a collection of basic functions that may help a bit more than the famed piece of code.</p>
<pre>######################################################
############### Hello world functions ################
######################################################
##################################
# General info
fun &lt;- function( arguments ) { body }


##################################
foo.add &lt;- function(x,y){
  x+y
}

foo.add(<span>7</span>, <span>5</span>)

----------------------------------

foo.above &lt;- function(x){
  x[x><span>10</span>]
}

foo.above(<span>1</span>:<span>100</span>)

----------------------------------

foo.above_n &lt;- function(x,n){
  x[x>n]
}

foo.above_n(<span>1</span>:<span>20</span>, <span>12</span>)

----------------------------------

foo = seq(<span>1</span>, <span>100</span>, <span>by</span>=<span>2</span>)
foo.squared = NULL

<span>for</span> (i <span>in</span> <span>1</span>:<span>50</span> ) {
  foo.squared[i] = foo[i]^<span>2</span>
}

foo.squared

----------------------------------

a &lt;- c(<span>1</span>,<span>6</span>,<span>7</span>,<span>8</span>,<span>8</span>,<span>9</span>,<span>2</span>)

s &lt;- <span>0</span>
<span>for</span> (i <span>in</span> <span>1</span>:length(a)){
  s &lt;- s + a[[i]]
}
s

----------------------------------

a &lt;- c(<span>1</span>,<span>6</span>,<span>7</span>,<span>8</span>,<span>8</span>,<span>9</span>,<span>2</span>,<span>100</span>)

s &lt;- <span>0</span>
i &lt;- <span>1</span>
<span>while</span> (i &lt;= length(a)){
  s &lt;- s + a[[i]]
  i &lt;- i+<span>1</span>
}
s

----------------------------------

FunSum &lt;- function(a){
  s &lt;- <span>0</span>
  i &lt;- <span>1</span>
  <span>while</span> (i &lt;= length(a)){
    s &lt;- s + a[[i]]
    i &lt;- i+<span>1</span>
  }
  <span>print</span>(s)
}

FunSum(a)

-----------------------------------

SumInt &lt;- function(n){
  s &lt;- <span>0</span>
  <span>for</span> (i <span>in</span> <span>1</span>:n){
    s &lt;- s + i
  }
  <span>print</span>(s)  
}

SumInt(<span>14</span>)

-----------------------------------
# find the maximum
# right to left assignment
x &lt;- c(<span>3</span>, <span>9</span>, <span>7</span>, <span>2</span>)

# trick: it is necessary to use a temporary variable to allow the comparison by pairs of
# each number of the sequence, i.e. the process of comparison is incremental: each time
# a bigger number compared to the previous in the sequence is found, it is assigned as the
# temporary maximum
# Since the process has to start somewhere, the first (temporary) maximum is assigned to be
# the first number of the sequence

max &lt;- x[<span>1</span>]
<span>for</span>(i <span>in</span> x){
  tmpmax = i
  <span>if</span>(tmpmax > max){
    max = tmpmax
  }
}

max

x &lt;- c(<span>-20</span>, <span>-14</span>, <span>6</span>, <span>2</span>)
x &lt;- c(<span>-2</span>, <span>-24</span>, <span>-14</span>, <span>-7</span>)

min &lt;- x[<span>1</span>]
<span>for</span>(i <span>in</span> x){
  tmpmin = i
  <span>if</span>(tmpmin &lt; min){
    min = tmpmin
  }
}

min

----------------------------------
# n is the nth Fibonacci number
# temp is the temporary variable

Fibonacci &lt;- function(n){
  a &lt;- <span>0</span>
  b &lt;- <span>1</span>
  <span>for</span>(i <span>in</span> <span>1</span>:n){
    temp &lt;- b
    b &lt;- a
    a &lt;- a + temp
  }
  <span>return</span>(a)
}

Fibonacci(<span>13</span>)

----------------------------------
# R available factorial function
factorial(<span>5</span>)

# recursive function: ff
ff &lt;- function(x) {
  <span>if</span>(x&lt;=<span>0</span>) {
    <span>return</span>(<span>1</span>)
  } <span>else</span> {
    <span>return</span>(x*ff(x<span>-1</span>)) # function uses the fact it knows its own name to call itself
  }
}
ff(<span>5</span>)

----------------------------------

say_hello_to &lt;- function(name){
  paste(<span>&quot;Hello&quot;</span>, name)
} 
say_hello_to(<span>&quot;Roberto&quot;</span>)

----------------------------------

foo.colmean &lt;- function(y){
  nc &lt;- ncol(y)
  means &lt;- numeric(nc)
  <span>for</span>(i <span>in</span> <span>1</span>:nc){
    means[i] &lt;- mean(y[,i])
  }
  means
}

foo.colmean(airquality)

----------------------------------

foo.colmean &lt;- function(y, removeNA=FALSE){
  nc &lt;- ncol(y)
  means &lt;- numeric(nc)
  <span>for</span>(i <span>in</span> <span>1</span>:nc){
    means[i] &lt;- mean(y[,i], na.rm=removeNA)
  }
  means
}

foo.colmean(airquality, TRUE)

----------------------------------

foo.contingency &lt;- function(x,y){
  nc &lt;- ncol(x)
  out &lt;- list() 
  <span>for</span> (i <span>in</span> <span>1</span>:nc){
    out[[i]] &lt;- table(y, x[,i]) 
  }
  names(out) &lt;- names(x)
  out
}

set.seed(<span>123</span>)
v1 &lt;- sample(c(rep(<span>&quot;a&quot;</span>, <span>5</span>), rep(<span>&quot;b&quot;</span>, <span>15</span>), rep(<span>&quot;c&quot;</span>, <span>20</span>)))
v2 &lt;- sample(c(rep(<span>&quot;d&quot;</span>, <span>15</span>), rep(<span>&quot;e&quot;</span>, <span>20</span>), rep(<span>&quot;f&quot;</span>, <span>5</span>)))
v3 &lt;- sample(c(rep(<span>&quot;g&quot;</span>, <span>10</span>), rep(<span>&quot;h&quot;</span>, <span>10</span>), rep(<span>&quot;k&quot;</span>, <span>20</span>)))

data &lt;- data.frame(v1, v2, v3)

foo.contingency(data,v3)
</pre>
<pre></pre>
<p>That’s all folks!</p>
<p>#R #rstats #maRche #Rbloggers</p>
<p><em>This post is also shared in <a rel="nofollow" href="https://www.linkedin.com/pulse/print-hello-enough-collection-world-functions-roberto-palloni/" target="_blank">LinkedIn</a> and </em><a rel="nofollow" href="https://www.r-bloggers.com/" target="_blank"><em>www.r-bloggers.com</em></a></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-posts.com/print-hello%E2%80%8B-is-not-enough-a-collection-of-hello-world-functions/"> R-posts.com</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/-AvBmw8xOTY" height="1"></a></div>