An R mini-tutorial/reference

An R mini-tutorial/reference

One of the shortcomings of R is that there are typically 4-5 ways to do the same thing. For people that are relative beginners, such as me. It is not at all obvious, it’s not immediately obvious which one is the optimal way to do things(there are a whopping FOUR systems for dealing with OOP for example). Furthremore, R functions tends to that are not intuitive.

In this post, I’ll try to get to know a couple of functions that I think will play nice with each other and that I can use over and over again. It might not be the 100% optimal choice but the most important part is to have something working at the end of the day.

I’ll update and add on this post as I go along and encounter new things.

Pipes ! Pipes ! Pipes!

The very first video game I ever played was Super Mario bros. It’s the story of Mario, an overweight italian plumber who tries to save a Princess and her underlings that are as numerous as they are helpless. In mario bros, there are pipes that you can use to skip to other levels if you want to:

What does this have to do with the subject at hand? Beside the name, nothing. I just wanted to reference Super mario bros.

Well kind of.

Datacamp has an excellent tutorial on the subject, and so I won’t repeat what they said.

In a nutshell, Pipes are syntactic sugar for function composition e.g. f(g(h(x))) is the same as h(x) >%> g() %>% f(). What it does is that it passes along x to h, then the output of h to g then… etc.

So yeah in that sense they do make life easier but nowhere near to the extent that Super Mario pipes did, sadly.

I’ll only note that the pipe is by nature linear and unary: meaning it works best when we’re only passing along one argument. In R, the pipe pass along the output of the last function unto the first argument of the next by default.

But we can use the placeholder “.” to specifically in which argument to pass on the output also. E.g. suppose that we want to pass.

From the link above:

e.g. # The nested function call with dot placeholder

1:5 %>%
  paste(., letters[.])
  1. ‘1 a’
  2. ‘2 b’
  3. ‘3 c’
  4. ‘4 d’

Also, if you need to have side effects or use an impure function somewhere in your pipeline (e.g. you want to print a graph, which returns nothing) then you can use the %T>% operator. Think of it as a skip.

Say you have f %>% g %T>% h with g being something that does not return anything. What %T>% does is to pass on the ouput of f to h directly.

Anonymous functions in R

See section 9.2.2 of this article

Think of it as functions that are so one-off and short as to not deserve a name. Here purr has a nice shortcut: the tilde operator

E.g. ~ min(.x) + max(.x)

is the same as function(x) { return (min(x) + max(x))}


Grepl function in R

Grep is something that comes from Linux. Essentially , it returns whether a string contains a certain pattern.Catch: the second argument is the data, and not the first.

Starting to put this into practice

I tried a bunch of different things using apply,

It turns out, for strings colunmns, it returns the type “factor”. Apparently this has advantages for the statisticians but unfortunately of a boring run-of-the-mill string.

I came across this tidbit in this ebook:

“Never use apply() with a data frame. It always coerces it to a matrix, which will lead to undesirable results if your data frame contains anything other than numbers.”

Which is exactly the solution to my problems.

In turn,it led me to the purr library and the map() function.

It states as a selling point:

  • The first argument is always the data, so purrr works naturally with the pipe.
  • All purrr functions are type-stable. They always return the advertised output type (map() returns lists; map_dbl() returns double vectors), or they throw an error.
  • All map() functions either accept function, formulas (used for succinctly generating anonymous functions), a character vector (used to extract components by name), or a numeric vector (used to extract by position).

Going once… Going twice… sold!

Anyways long enough for a blog post.

See you later!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: