This blog post is an excerpt of my ebook Modern R with the tidyverse that you can read for
free here. This is taken from Chapter 2, which explains
the different R objects you can manipulate as well as some functions to get you started.
Objects, types and useful R functions to get started
All objects in R have a given type. You already know most of them, as these types are also used
in mathematics. Integers, floating point numbers, or floats, matrices, etc, are all objects you
are already familiar with. But R has other, maybe lesser known data types (that you can find in a
lot of other programming languages) that you need to become familiar with. But first, we need to
learn how to assign a value to a variable. This can be done in two ways:
a You can also use the str() function:
str(my_data)
And if you need to access an individual column, you can use the $ sign, same as for a list:
my_data$col1
Formulas
We will learn more about formulas later, but because it is an important object, it is useful if you
already know about them early on. A formula is defined in the following way:
my_formula 5, 1, &quot;this is false&quot;)
## [1] &quot;this is false&quot;
and compare it to {dplyr}’s implementation, if_else():
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
if_else() fails because the return value when FALSE is not a double (a real number) but a character. This might seem unnecessarily
strict, but at least it is predictable. This makes debugging easier when used inside functions. In Chapter 8 we are going to learn how
to write our own functions, and being strict makes programming easier.
Basic string manipulation
For now, we have not closely studied character objects, we only learned how to define them. Later, in Chapter 5 we will learn about the
{stringr} package which provides useful function to work with strings. However, there are several base R functions that are very
useful that you might want to know nonetheless, such as paste() and paste0():
paste(&quot;Hello&quot;, &quot;amigo&quot;)
## [1] &quot;Hello amigo&quot;
but you can also change the separator if needed:
paste(&quot;Hello&quot;, &quot;amigo&quot;, sep = &quot;--&quot;)
## [1] &quot;Hello--amigo&quot;
paste0() is the same as paste() but does not have any sep argument:
paste0(&quot;Hello&quot;, &quot;amigo&quot;)
## [1] &quot;Helloamigo&quot;
If you provide a vector of characters, you can also use the collapse argument, which places whatever you provide for collapse between the
characters of the vector:
paste0(c(&quot;Joseph&quot;, &quot;Mary&quot;, &quot;Jesus&quot;), collapse = &quot;, and &quot;)
## [1] &quot;Joseph, and Mary, and Jesus&quot;
To change the case of characters, you can use toupper() and tolower():
tolower(&quot;HAHAHAHAH&quot;)
## [1] &quot;hahahahah&quot;
toupper(&quot;hueuehuehuheuhe&quot;)
## [1] &quot;HUEUEHUEHUHEUHE&quot;
Mathematical functions
Finally, there are the classical mathematical functions that you know and love:
sqrt()
exp()
log()
abs()
sin(), cos(), tan(), and others
sum(), cumsum(), prod(), cumprod()
max(), min()
and many others…
Hope you enjoyed! If you found this blog post useful, you might want to follow
me on twitter for blog post updates and
buy me an espresso or paypal.me.
Buy me an Espresso