<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/objects-types-and-some-useful-r-functions-for-beginners/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.brodrigues.co/blog/2018-12-24-modern_objects/"> Econometrics and Free Software</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><a rel="nofollow" href="https://www.youtube.com/watch?v=M-1nTwiHxic" target="_blank"><br>
<img width="400" title="The frydiest time of the year" src="https://i0.wp.com/www.brodrigues.co/blog/2018-12-24-modern_objects/img/santa_sanders.jpg?w=456" data-recalc-dims="1"></a></p>
</div>
<p></p>
<p>This blog post is an excerpt of my ebook <em>Modern R with the tidyverse</em> that you can read for<br>
free <a rel="nofollow" href="https://b-rodrigues.github.io/modern_R/" target="_blank">here</a>. This is taken from Chapter 2, which explains<br>
the different R objects you can manipulate as well as some functions to get you started.</p>
<div>
<h2>Objects, types and useful R functions to get started</h2>
<p>All objects in R have a given <em>type</em>. You already know most of them, as these types are also used<br>
in mathematics. Integers, floating point numbers, or floats, matrices, etc, are all objects you<br>
are already familiar with. But R has other, maybe lesser known data types (that you can find in a<br>
lot of other programming languages) that you need to become familiar with. But first, we need to<br>
learn how to assign a value to a variable. This can be done in two ways:</p>
<pre><code>a &lt;- 3</code></pre>
<p>or</p>
<pre><code>a = 3</code></pre>
<p>in very practical terms, there is no difference between the two. I prefer using <code>&lt;-</code> for assigning<br>
values to variables and reserve <code>=</code> for passing arguments to functions, for example:</p>
<pre><code>spam &lt;- mean(x = c(1,2,3))</code></pre>
<p>I think this is less confusing than:</p>
<pre><code>spam = mean(x = c(1,2,3))</code></pre>
<p>but as I explained above you can use whatever you feel most comfortable with.</p>
<div>
<h3>The <code>numeric</code> class</h3>
<p>To define single numbers, you can do the following:</p>
<pre><code>a &lt;- 3</code></pre>
<p>The <code>class()</code> function allows you to check the class of an object:</p>
<pre><code>class(a)</code></pre>
<pre><code>## [1] &quot;numeric&quot;</code></pre>
<p>Decimals are defined with the character <code>.</code>:</p>
<pre><code>a &lt;- 3.14</code></pre>
<p>R also supports integers. If you find yourself in a situation where you explicitly need an integer<br>
and not a floating point number, you can use the following:</p>
<pre><code>a  &lt;- as.integer(3)
class(a)</code></pre>
<pre><code>## [1] &quot;integer&quot;</code></pre>
<p>The <code>as.integer()</code> function is very useful, because it converts its argument into an integer. There<br>
is a whole family of <code>as.*()</code> functions. To convert <code>a</code> into a floating point number again:</p>
<pre><code>class(as.numeric(a))</code></pre>
<pre><code>## [1] &quot;numeric&quot;</code></pre>
<p>There is also <code>is.numeric()</code> which tests whether a number is of the <code>numeric</code> class:</p>
<pre><code>is.numeric(a)</code></pre>
<pre><code>## [1] TRUE</code></pre>
<p>These functions are very useful, there is one for any of the supported types in R. Later, we are going<br>
to learn about the <code>{purrr}</code> package, which is a very powerful package for functional programming. This<br>
package includes further such functions.</p>
</div>
<div>
<h3>The <code>character</code> class</h3>
<p>Use <code>&quot; &quot;</code> to define characters (called strings in other programming languages):</p>
<pre><code>a &lt;- &quot;this is a string&quot;</code></pre>
<pre><code>class(a)</code></pre>
<pre><code>## [1] &quot;character&quot;</code></pre>
<p>To convert something to a character you can use the <code>as.character()</code> function:</p>
<pre><code>a &lt;- 4.392

class(a)</code></pre>
<pre><code>## [1] &quot;numeric&quot;</code></pre>
<pre><code>class(as.character(a))</code></pre>
<pre><code>## [1] &quot;character&quot;</code></pre>
<p>It is also possible to convert a character to a numeric:</p>
<pre><code>a &lt;- &quot;4.392&quot;

class(a)</code></pre>
<pre><code>## [1] &quot;character&quot;</code></pre>
<pre><code>class(as.numeric(a))</code></pre>
<pre><code>## [1] &quot;numeric&quot;</code></pre>
<p>But this only works if it makes sense:</p>
<pre><code>a &lt;- &quot;this won't work, chief&quot;

class(a)</code></pre>
<pre><code>## [1] &quot;character&quot;</code></pre>
<pre><code>as.numeric(a)</code></pre>
<pre><code>## Warning: NAs introduced by coercion</code></pre>
<pre><code>## [1] NA</code></pre>
<p>A very nice package to work with characters is <code>{stringr}</code>, which is also part of the <code>{tidyverse}</code>.</p>
</div>
<div>
<h3>The <code>factor</code> class</h3>
<p>Factors look like characters, but are very different. They are the representation of categorical<br>
variables. A <code>{tidyverse}</code> package to work with factors is <code>{forcats}</code>. You would rarely use<br>
factor variables outside of datasets, so for now, it is enough to know that this class exists.<br>
We are going to learn more about factor variables in Chapter 4, by using the <code>{forcats}</code> package.</p>
</div>
<div>
<h3>The <code>Date</code> class</h3>
<p>Dates also look like characters, but are very different too:</p>
<pre><code>as.Date(&quot;2019/03/19&quot;)</code></pre>
<pre><code>## [1] &quot;2019-03-19&quot;</code></pre>
<pre><code>class(as.Date(&quot;2019/03/19&quot;))</code></pre>
<pre><code>## [1] &quot;Date&quot;</code></pre>
<p>Manipulating dates and time can be tricky, but thankfully there’s a <code>{tidyverse}</code> package for that,<br>
called <code>{lubridate}</code>. We are going to go over this package in Chapter 4.</p>
</div>
<div>
<h3>The <code>logical</code> class</h3>
<p>This class is the result of logical comparisons, for example, if you type:</p>
<pre><code>4 > 3</code></pre>
<pre><code>## [1] TRUE</code></pre>
<p>R returns <code>TRUE</code>, which is an object of class <code>logical</code>:</p>
<pre><code>k &lt;- 4 > 3
class(k)</code></pre>
<pre><code>## [1] &quot;logical&quot;</code></pre>
<p>In other programming languages, <code>logical</code>s are often called <code>bool</code>s. A <code>logical</code> variable can only have<br>
two values, either <code>TRUE</code> or <code>FALSE</code>. You can test the truthiness of a variable with <code>isTRUE()</code>:</p>
<pre><code>k &lt;- 4 > 3
isTRUE(k)</code></pre>
<pre><code>## [1] TRUE</code></pre>
<p>How can you test if a variable is false? There is not a <code>isFALSE()</code> function (at least not without having<br>
to load a package containing this function), but there is way to do it:</p>
<pre><code>k &lt;- 4 > 3
!isTRUE(k)</code></pre>
<pre><code>## [1] FALSE</code></pre>
<p>The <code>!</code> operator indicates negation, so the above expression could be translated as <em>is k not TRUE?</em>.<br>
There are other such operators, namely <code>&amp;, &amp;&amp;, |, ||</code>. <code>&amp;</code> means <em>and</em> and <code>|</code> stands for <em>or</em>.<br>
You might be wondering what the difference between <code>&amp;</code> and <code>&amp;&amp;</code> is? Or between <code>|</code> and <code>||</code>? <code>&amp;</code> and<br>
<code>|</code> work on vectors, doing pairwise comparisons:</p>
<pre><code>one &lt;- c(TRUE, FALSE, TRUE, FALSE)
two &lt;- c(FALSE, TRUE, TRUE, TRUE)
one &amp; two</code></pre>
<pre><code>## [1] FALSE FALSE  TRUE FALSE</code></pre>
<p>Compare this to the <code>&amp;&amp;</code> operator:</p>
<pre><code>one &lt;- c(TRUE, FALSE, TRUE, FALSE)
two &lt;- c(FALSE, TRUE, TRUE, TRUE)
one &amp;&amp; two</code></pre>
<pre><code>## [1] FALSE</code></pre>
<p>The <code>&amp;&amp;</code> and <code>||</code> operators only compare the first element of the vectors and stop as soon as a the return<br>
value can be safely determined. This is called short-circuiting. Consider the following:</p>
<pre><code>one &lt;- c(TRUE, FALSE, TRUE, FALSE)
two &lt;- c(FALSE, TRUE, TRUE, TRUE)
three &lt;- c(TRUE, TRUE, FALSE, FALSE)
one &amp;&amp; two &amp;&amp; three</code></pre>
<pre><code>## [1] FALSE</code></pre>
<pre><code>one || two || three</code></pre>
<pre><code>## [1] TRUE</code></pre>
<p>The <code>||</code> operator stops as soon it evaluates to <code>TRUE</code> whereas the <code>&amp;&amp;</code> stops as soon as it evaluates to <code>FALSE</code>.<br>
Personally, I rarely use <code>||</code> or <code>&amp;&amp;</code> because I get confused. I find using <code>|</code> or <code>&amp;</code> in combination with the<br>
<code>all()</code> or <code>any()</code> functions much more useful:</p>
<pre><code>one &lt;- c(TRUE, FALSE, TRUE, FALSE)
two &lt;- c(FALSE, TRUE, TRUE, TRUE)
any(one &amp; two)</code></pre>
<pre><code>## [1] TRUE</code></pre>
<pre><code>all(one &amp; two)</code></pre>
<pre><code>## [1] FALSE</code></pre>
<p><code>any()</code> checks whether any of the vector’s elements are <code>TRUE</code> and <code>all()</code> checks if all elements of the vector are<br>
<code>TRUE</code>.</p>
<p>As a final note, you should know that is possible to use <code>T</code> for <code>TRUE</code> and <code>F</code> for <code>FALSE</code> but I would advise against<br>
doing this, because it is not very explicit.</p>
</div>
<div>
<h3>Vectors and matrices</h3>
<p>You can create a vector in different ways. But first of all, it is important to understand that a<br>
vector in most programming languages is nothing more than a list of things. These things can be<br>
numbers (either integers or floats), strings, or even other vectors. A vector in R can only contain elements of one<br>
single type. This is not the case for a list, which is much more flexible. We will talk about lists shortly, but<br>
let’s first focus on vectors and matrices.</p>
<div>
<h4>The <code>c()</code> function</h4>
<p>A very important function that allows you to build a vector is <code>c()</code>:</p>
<pre><code>a &lt;- c(1,2,3,4,5)</code></pre>
<p>This creates a vector with elements 1, 2, 3, 4, 5. If you check its class:</p>
<pre><code>class(a)</code></pre>
<pre><code>## [1] &quot;numeric&quot;</code></pre>
<p>This can be confusing: you where probably expecting a to be of class <em>vector</em> or<br>
something similar. This is not the case if you use <code>c()</code> to create the vector, because <code>c()</code><br>
doesn’t build a vector in the mathematical sense, but a so-called atomic vector.<br>
Checking its dimension:</p>
<pre><code>dim(a)</code></pre>
<pre><code>## NULL</code></pre>
<p>returns <code>NULL</code> because an atomic vector doesn’t have a dimension.<br>
If you want to create a true vector, you need to use <code>cbind()</code> or <code>rbind()</code>.</p>
<p>But before continuing, be aware that atomic vectors can only contain elements of the same type:</p>
<pre><code>c(1, 2, &quot;3&quot;)</code></pre>
<pre><code>## [1] &quot;1&quot; &quot;2&quot; &quot;3&quot;</code></pre>
<p>because “3” is a character, all the other values get implicitly converted to characters. You have<br>
to be very careful about this, and if you use atomic vectors in your programming, you have to make<br>
absolutely sure that no characters or logicals or whatever else are going to convert your atomic<br>
vector to something you were not expecting.</p>
</div>
<div>
<h4><code>cbind()</code> and <code>rbind()</code></h4>
<p>You can create a <em>true</em> vector with <code>cbind()</code>:</p>
<pre><code>a &lt;- cbind(1, 2, 3, 4, 5)</code></pre>
<p>Check its class now:</p>
<pre><code>class(a)</code></pre>
<pre><code>## [1] &quot;matrix&quot;</code></pre>
<p>This is exactly what we expected. Let’s check its dimension:</p>
<pre><code>dim(a)</code></pre>
<pre><code>## [1] 1 5</code></pre>
<p>This returns the dimension of <code>a</code> using the LICO notation (number of LInes first, the number of COlumns).</p>
<p>It is also possible to bind vectors together to create a matrix.</p>
<pre><code>b &lt;- cbind(6,7,8,9,10)</code></pre>
<p>Now let’s put vector <code>a</code> and <code>b</code> into a matrix called <code>matrix_c</code> using <code>rbind()</code>.<br>
<code>rbind()</code> functions the same way as <code>cbind()</code> but glues the vectors together by rows and not by columns.</p>
<pre><code>matrix_c &lt;- rbind(a,b)
print(matrix_c)</code></pre>
<pre><code>##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5
## [2,]    6    7    8    9   10</code></pre>
</div>
<div>
<h4>The <code>matrix</code> class</h4>
<p>R also has support for matrices. For example, you can create a matrix of dimension (5,5) filled<br>
with 0’s with the <code>matrix()</code> function:</p>
<pre><code>matrix_a &lt;- matrix(0, nrow = 5, ncol = 5)</code></pre>
<p>If you want to create the following matrix:</p>
<p><span>\[<br>
B = \left(<br>
\begin{array}{ccc}<br>
2 &amp; 4 &amp; 3 \\<br>
1 &amp; 5 &amp; 7<br>
\end{array} \right)<br>
\]</span></p>
<p>you would do it like this:</p>
<pre><code>B &lt;- matrix(c(2, 4, 3, 1, 5, 7), nrow = 2, byrow = TRUE)</code></pre>
<p>The option <code>byrow = TRUE</code> means that the rows of the matrix will be filled first.</p>
<p>You can access individual elements of <code>matrix_a</code> like so:</p>
<pre><code>matrix_a[2, 3]</code></pre>
<pre><code>## [1] 0</code></pre>
<p>and R returns its value, 0. We can assign a new value to this element if we want. Try:</p>
<pre><code>matrix_a[2, 3] &lt;- 7</code></pre>
<p>and now take a look at <code>matrix_a</code> again.</p>
<pre><code>print(matrix_a)</code></pre>
<pre><code>##      [,1] [,2] [,3] [,4] [,5]
## [1,]    0    0    0    0    0
## [2,]    0    0    7    0    0
## [3,]    0    0    0    0    0
## [4,]    0    0    0    0    0
## [5,]    0    0    0    0    0</code></pre>
<p>Recall our vector <code>b</code>:</p>
<pre><code>b &lt;- cbind(6,7,8,9,10)</code></pre>
<p>To access its third element, you can simply write:</p>
<pre><code>b[3]</code></pre>
<pre><code>## [1] 8</code></pre>
<p>I have heard many people praising R for being a matrix based language. Matrices are indeed useful,<br>
and statisticians are used to working with them. However, I very rarely use matrices in my<br>
day to day work, and prefer an approach based on data frames (which will be discussed below). This<br>
is because working with data frames makes it easier to use R’s advanced functional programming<br>
language capabilities, and this is where R really shines in my opinion. Working with matrices<br>
almost automatically implies using loops and all the iterative programming techniques, <em>à la Fortran</em>,<br>
which I personally believe are ill-suited for interactive statistical programming (as discussed in<br>
the introduction).</p>
</div>
</div>
<div>
<h3>The <code>list</code> class</h3>
<p>The <code>list</code> class is a very flexible class, and thus, very useful. You can put anything inside a list,<br>
such as numbers:</p>
<pre><code>list1 &lt;- list(3, 2)</code></pre>
<p>or other lists constructed with <code>c()</code>:</p>
<pre><code>list2 &lt;- list(c(1, 2), c(3, 4))</code></pre>
<p>you can also put objects of different classes in the same list:</p>
<pre><code>list3 &lt;- list(3, c(1, 2), &quot;lists are amazing!&quot;)</code></pre>
<p>and of course create list of lists:</p>
<pre><code>my_lists &lt;- list(list1, list2, list3)</code></pre>
<p>To check the contents of a list, you can use the structure function <code>str()</code>:</p>
<pre><code>str(my_lists)</code></pre>
<pre><code>## List of 3
##  $ :List of 2
##   ..$ : num 3
##   ..$ : num 2
##  $ :List of 2
##   ..$ : num [1:2] 1 2
##   ..$ : num [1:2] 3 4
##  $ :List of 3
##   ..$ : num 3
##   ..$ : num [1:2] 1 2
##   ..$ : chr &quot;lists are amazing!&quot;</code></pre>
<p>or you can use RStudio’s <em>Environment</em> pane:</p>
<p><img src="https://i0.wp.com/www.brodrigues.co/blog/2018-12-24-modern_objects/img/rstudio_environment_list.gif?w=456" data-recalc-dims="1"></p>
<p>You can also create named lists:</p>
<pre><code>list4 &lt;- list(&quot;a&quot; = 2, &quot;b&quot; = 8, &quot;c&quot; = &quot;this is a named list&quot;)</code></pre>
<p>and you can access the elements in two ways:</p>
<pre><code>list4[[1]]</code></pre>
<pre><code>## [1] 2</code></pre>
<p>or, for named lists:</p>
<pre><code>list4$c</code></pre>
<pre><code>## [1] &quot;this is a named list&quot;</code></pre>
<p>Lists are used extensively because they are so flexible. You can build lists of datasets and apply<br>
functions to all the datasets at once, build lists of models, lists of plots, etc… In the later<br>
chapters we are going to learn all about them. Lists are central objects in a functional programming<br>
workflow for interactive statistical analysis.</p>
</div>
<div>
<h3>The <code>data.frame</code> and <code>tibble</code> classes</h3>
<p>In the next chapter we are going to learn how to import datasets into R. Once you import data, the<br>
resulting object is either a <code>data.frame</code> or a <code>tibble</code> depending on which package you used to<br>
import the data. <code>tibble</code>s extend <code>data.frame</code>s so if you know about <code>data.frame</code> objects already,<br>
working with <code>tibble</code>s will be very easy. <code>tibble</code>s have a better <code>print()</code> method, and some other<br>
niceties.</p>
<p>However, I want to stress that these objects are central to R and are thus very important; they are<br>
actually special cases of lists, discussed above. There are different ways to print a <code>data.frame</code> or<br>
a <code>tibble</code> if you wish to inspect it. You can use <code>View(my_data)</code> to show the <code>my_data</code> <code>data.frame</code><br>
in the <em>View</em> pane of RStudio:</p>
<p><img src="https://i0.wp.com/www.brodrigues.co/blog/2018-12-24-modern_objects/img/rstudio_view_data.gif?w=456" data-recalc-dims="1"></p>
<p>You can also use the <code>str()</code> function:</p>
<pre><code>str(my_data)</code></pre>
<p>And if you need to access an individual column, you can use the <code>$</code> sign, same as for a list:</p>
<pre><code>my_data$col1</code></pre>
</div>
<div>
<h3>Formulas</h3>
<p>We will learn more about formulas later, but because it is an important object, it is useful if you<br>
already know about them early on. A formula is defined in the following way:</p>
<pre><code>my_formula &lt;- ~x

class(my_formula)</code></pre>
<pre><code>## [1] &quot;formula&quot;</code></pre>
<p>Formula objects are defined using the <code>~</code> symbol. Formulas are useful to define statistical models,<br>
for example for a linear regression:</p>
<pre><code>lm(y ~ x)</code></pre>
<p>or also to define anonymous functions, but more on this later.</p>
</div>
</div>
<div>
<h2>Models</h2>
<p>A statistical model is an object like any other in R:</p>
<pre><code>data(mtcars)

my_model &lt;- lm(mpg ~ hp, mtcars)

class(my_model)</code></pre>
<pre><code>## [1] &quot;lm&quot;</code></pre>
<p><code>my_model</code> is an object of class <code>lm</code>. You can apply different functions to a model object:</p>
<pre><code>summary(my_model)</code></pre>
<pre><code>## 
## Call:
## lm(formula = mpg ~ hp, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.7121 -2.1122 -0.8854  1.5819  8.2360 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 30.09886    1.63392  18.421  &lt; 2e-16 ***
## hp          -0.06823    0.01012  -6.742 1.79e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.863 on 30 degrees of freedom
## Multiple R-squared:  0.6024, Adjusted R-squared:  0.5892 
## F-statistic: 45.46 on 1 and 30 DF,  p-value: 1.788e-07</code></pre>
<p>This class will be explored in later chapters.</p>
<div>
<h3>NULL, NA and NaN</h3>
<p>The <code>NULL</code>, <code>NA</code> and <code>NaN</code> classes are pretty special. <code>NULL</code> is returned when the result of function is undetermined.<br>
For example, consider <code>list4</code>:</p>
<pre><code>list4</code></pre>
<pre><code>## $a
## [1] 2
## 
## $b
## [1] 8
## 
## $c
## [1] &quot;this is a named list&quot;</code></pre>
<p>if you try to access an element that does not exist, such as <code>d</code>, you will get <code>NULL</code> back:</p>
<pre><code>list4$d</code></pre>
<pre><code>## NULL</code></pre>
<p><code>NaN</code> means “Not a Number” and is returned when a function return something that is not a number:</p>
<pre><code>sqrt(-1)</code></pre>
<pre><code>## Warning in sqrt(-1): NaNs produced</code></pre>
<pre><code>## [1] NaN</code></pre>
<p>or:</p>
<pre><code>0/0</code></pre>
<pre><code>## [1] NaN</code></pre>
<p>Basically, numbers that cannot be represented as floating point numbers are <code>NaN</code>.</p>
<p>Finally, there’s <code>NA</code> which is closely related to <code>NaN</code> but is used for missing values. <code>NA</code> stands for <code>Not Available</code>. There are<br>
several types of <code>NA</code>s:</p>
<ul>
<li><code>NA_integer_</code></li>
<li><code>NA_real_</code></li>
<li><code>NA_complex_</code></li>
<li><code>NA_character_</code></li>
</ul>
<p>but these are in principle only used when you need to program your own functions and need to explicitly test for the missingness of, say,<br>
a character value.</p>
<p>To test whether a value is <code>NA</code>, use the <code>is.na()</code> function.</p>
</div>
<div>
<h3>Useful functions to get you started</h3>
<p>This section will list several basic R functions that are very useful and should be part of your toolbox.</p>
<div>
<h4>Sequences</h4>
<p>There are several functions that create sequences, <code>seq()</code>, <code>seq_along()</code> and <code>rep()</code>. <code>rep()</code> is easy enough:</p>
<pre><code>rep(1, 10)</code></pre>
<pre><code>##  [1] 1 1 1 1 1 1 1 1 1 1</code></pre>
<p>This simply repeats <code>1</code> 10 times. You can repeat other objects too:</p>
<pre><code>rep(&quot;HAHA&quot;, 10)</code></pre>
<pre><code>##  [1] &quot;HAHA&quot; &quot;HAHA&quot; &quot;HAHA&quot; &quot;HAHA&quot; &quot;HAHA&quot; &quot;HAHA&quot; &quot;HAHA&quot; &quot;HAHA&quot; &quot;HAHA&quot; &quot;HAHA&quot;</code></pre>
<p>To create a sequence, things are not as straightforward. There is <code>seq()</code>:</p>
<pre><code>seq(1, 10)</code></pre>
<pre><code>##  [1]  1  2  3  4  5  6  7  8  9 10</code></pre>
<pre><code>seq(70, 80)</code></pre>
<pre><code>##  [1] 70 71 72 73 74 75 76 77 78 79 80</code></pre>
<p>It is also possible to provide a <code>by</code> argument:</p>
<pre><code>seq(1, 10, by = 2)</code></pre>
<pre><code>## [1] 1 3 5 7 9</code></pre>
<p><code>seq_along()</code> behaves similarly, but returns the length of the object passed to it. So if you pass <code>list4</code> to<br>
<code>seq_along()</code>, it will return a sequence from 1 to 3:</p>
<pre><code>seq_along(list4)</code></pre>
<pre><code>## [1] 1 2 3</code></pre>
<p>which is also true for <code>seq()</code> actually:</p>
<pre><code>seq(list4)</code></pre>
<pre><code>## [1] 1 2 3</code></pre>
<p>but these two functions behave differently for arguments of length equal to 1:</p>
<pre><code>seq(10)</code></pre>
<pre><code>##  [1]  1  2  3  4  5  6  7  8  9 10</code></pre>
<pre><code>seq_along(10)</code></pre>
<pre><code>## [1] 1</code></pre>
<p>So be quite careful about that. I would advise you do not use <code>seq()</code>, but only <code>seq_along()</code> and <code>seq_len()</code>. <code>seq_len()</code><br>
only takes arguments of length 1:</p>
<pre><code>seq_len(10)</code></pre>
<pre><code>##  [1]  1  2  3  4  5  6  7  8  9 10</code></pre>
<pre><code>seq_along(10)</code></pre>
<pre><code>## [1] 1</code></pre>
<p>The problem with <code>seq()</code> is that it is unpredictable; depending on its input, the output will either be an integer or a sequence.<br>
When programming, it is better to have function that are stricter and fail when confronted to special cases, instead of returning<br>
some result. This is a bit of a recurrent issue with R, and the functions from the <code>{tidyverse}</code> mitigate this issue by being<br>
stricter than their base R counterparts. For example, consider the <code>ifelse()</code> function from base R:</p>
<pre><code>ifelse(3 > 5, 1, &quot;this is false&quot;)</code></pre>
<pre><code>## [1] &quot;this is false&quot;</code></pre>
<p>and compare it to <code>{dplyr}</code>’s implementation, <code>if_else()</code>:</p>
<pre><code>if_else(3 > 5, 1, &quot;this is false&quot;)
Error: `false` must be type double, not character
Call `rlang::last_error()` to see a backtrace</code></pre>
<p><code>if_else()</code> fails because the return value when <code>FALSE</code> is not a double (a real number) but a character. This might seem unnecessarily<br>
strict, but at least it is predictable. This makes debugging easier when used inside functions. In Chapter 8 we are going to learn how<br>
to write our own functions, and being strict makes programming easier.</p>
</div>
<div>
<h4>Basic string manipulation</h4>
<p>For now, we have not closely studied <code>character</code> objects, we only learned how to define them. Later, in Chapter 5 we will learn about the<br>
<code>{stringr}</code> package which provides useful function to work with strings. However, there are several base R functions that are very<br>
useful that you might want to know nonetheless, such as <code>paste()</code> and <code>paste0()</code>:</p>
<pre><code>paste(&quot;Hello&quot;, &quot;amigo&quot;)</code></pre>
<pre><code>## [1] &quot;Hello amigo&quot;</code></pre>
<p>but you can also change the separator if needed:</p>
<pre><code>paste(&quot;Hello&quot;, &quot;amigo&quot;, sep = &quot;--&quot;)</code></pre>
<pre><code>## [1] &quot;Hello--amigo&quot;</code></pre>
<p><code>paste0()</code> is the same as <code>paste()</code> but does not have any <code>sep</code> argument:</p>
<pre><code>paste0(&quot;Hello&quot;, &quot;amigo&quot;)</code></pre>
<pre><code>## [1] &quot;Helloamigo&quot;</code></pre>
<p>If you provide a vector of characters, you can also use the <code>collapse</code> argument, which places whatever you provide for <code>collapse</code> between the<br>
characters of the vector:</p>
<pre><code>paste0(c(&quot;Joseph&quot;, &quot;Mary&quot;, &quot;Jesus&quot;), collapse = &quot;, and &quot;)</code></pre>
<pre><code>## [1] &quot;Joseph, and Mary, and Jesus&quot;</code></pre>
<p>To change the case of characters, you can use <code>toupper()</code> and <code>tolower()</code>:</p>
<pre><code>tolower(&quot;HAHAHAHAH&quot;)</code></pre>
<pre><code>## [1] &quot;hahahahah&quot;</code></pre>
<pre><code>toupper(&quot;hueuehuehuheuhe&quot;)</code></pre>
<pre><code>## [1] &quot;HUEUEHUEHUHEUHE&quot;</code></pre>
</div>
<div>
<h4>Mathematical functions</h4>
<p>Finally, there are the classical mathematical functions that you know and love:</p>
<ul>
<li><code>sqrt()</code></li>
<li><code>exp()</code></li>
<li><code>log()</code></li>
<li><code>abs()</code></li>
<li><code>sin()</code>, <code>cos()</code>, <code>tan()</code>, and others</li>
<li><code>sum()</code>, <code>cumsum()</code>, <code>prod()</code>, <code>cumprod()</code></li>
<li><code>max()</code>, <code>min()</code></li>
</ul>
<p>and many others…</p>
<p>Hope you enjoyed! If you found this blog post useful, you might want to follow<br>
me on <a rel="nofollow" href="https://www.twitter.com/brodriguesco" target="_blank">twitter</a> for blog post updates and<br>
<a rel="nofollow" href="https://www.buymeacoffee.com/brodriguesco" target="_blank">buy me an espresso</a> or <a rel="nofollow" href="http://www.brodrigues.co/blog/2018-12-24-modern_objects/paypal.me/brodriguesco" target="_blank">paypal.me</a>.</p>

<a rel="nofollow" href="https://www.buymeacoffee.com/brodriguesco" target="_blank"><img alt="Buy me an Espresso" src="https://www.buymeacoffee.com/assets/img/BMC-btn-logo.svg"><span>Buy me an Espresso</span></a>
</div>
</div>
</div>
	<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://www.brodrigues.co/blog/2018-12-24-modern_objects/"> Econometrics and Free Software</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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/ieI9wjWaRdM" height="1"></a></div>