THE BASICS OF THE BASICS

The basics of the basics

This is R.

R is a box.

R is a box that contains smaller boxes.

R is a box that contains smaller boxes that contain the items you’ll be interested in.

R uses what is called a “object oriented language”. It basically means it deals with boxes (“objects”), and knows how to handle the boxes based on their intrinsic characteristics. Let’s try it.

Open R, if not already open. You can see a lot of blabla that doesn’t really matter right now for us. What matters is this sign “>”, it’s the prompt sign. This is where you will enter your instructions. In the following codes, I will not include the prompt sign in order to facilitate copying-pasting (Why, yes, I know, I’m so nice – You’re welcome!).

First, type a number, then hit enter.

5
[1] 5

What R is doing right now, it is simply returning to you the information you gave him. Now, if we want to do something with this information, you might as well be tidy and put it in a box. How? Simply by giving it a name. To do so, we have to give the name ‘Roger’ followed by “<-” (which means ‘I’m putting what follows in a box named ‘Roger’, you can also use the sign “=”), and finally followed with what we want to put in this box.

For example:

Roger <- 3

No response from R? Yep, it’s not going to repeat every single thing you say! It would be tiring both for you and him! But we can simply check what is in our box by calling it: Roger!!!

Roger
[1] 3

Tadaa. Our box contains the number 3. Fantastic… (On a side note, be careful, R is case sensitive. “Roger” and “roger” are two different objects. More interestingly, we can now do things with our box. Let’s try to add things to it.

Roger+4
[1] 7

Wow! It’s almost magic. Now, remember, by default, R does not put things in boxes. You have to tell it to do so if you want to keep the results of your operations.

You can easily use the most common mathematical operations:

2+3       
[1] 5          
6-10           
[1] -4         
2.5*33         
[1] 82.5       
4/5            
[1] 0.8        
2^3            
[1] 8          
sqrt(9)        
[1] 3          
8^(1/3)        
[1] 2          
exp(3)         
[1] 20.08554   
log(20.08554)      # (by default, natural logarithm, but it's possible to change the base)
[1] 3  
10^2.17 
[1] 147.9108 
log10(147.9108)   # (common logarithm) 
[1] 2.17 
log(147.9108, base=10) 
[1] 2.17 
factorial(5)
[1] 120
21 %% 5 # (21 modulus 5)
[1] 1

Also, before going further, be careful, R is a little rude… If you try to put something in a box (object) that already exists, it will get rid of the old box (and everything it contains) without warnings, to replace it with the new information.

Roger
[1] 3
Roger <- Roger+4
Roger
[1] 7

See?

I see you, being all “So what? Big deal Big Cheese!” (because, yes, in this disillusion of mine, you’re calling me “Big Cheese”! Why do you call me that? Is that because I’m French? Now you are just being offensive!) Well, the big deal is that you can put pretty much anything in your box/object!

For example, let’s put several number (or elements) in one box. To do this, we are creating a string of elements (here, numbers) called a vector with the function “c()”, it’s (really) short for “concatenate”. Each element has to be separated by a comma, and R will keep in memory the order in which they appear in the object.

vec=c(2,3,10,-5,-0.33)

Our box/object “vec” now contains a vector, where subset elements are ordered in the way we entered them.

vec
[1]  2.00  3.00 10.00 -5.00 -0.33

From there, we can call the 4th element in our box by using the command ‘[]’ that allows you to select a specific element in a vector:

vec[4]
[1] -5

Now, we can do operations on each element of this object at the same time, simply by doing those operations on the ‘box’ (object) itself. Let’s try it:

vec*2
[1]   4.00   6.00  20.00 -10.00  -0.66
vec+10
[1] 12.00 13.00 20.00  5.00  9.67
vec2=c(10,20,30,40,50)
vec+vec2
[1] 12.00 23.00 40.00 35.00 49.67

Noticed how our vectors were added to each other with a one to one matching? The first element of vector “vec” was added to the first element of vector “vec2”, the second to the second and so on. (If one of the 2 vectors is shorter than the other, its values will be recycled.)

Are you starting to feel the potential of this program? You can easily do complex operations on multiple elements at the same time, all of that by simply typing a short line of code. What’s that boss? We got new data we can add to our datasets? We are going to extend this study for a couple more years? Ahahah I say! Ahahah indeed! No problem! I’ll simply add those to my current data, and can easily rerun all of our analyses by simply copying and pasting the code I’ve already used without having to point and click a bunch of menus and sub-menus (will you take a drink and chips with that?) all over again! So much saved time!

Another R trump card is the use of functions. Functions in R are essentially “boxes” that contain instructions about how to handle other boxes. To use them, you need to call them by their name (because it’s the polite thing to do) and follow it by the name of the object you want to work on between parentheses. Indications between parentheses are called “arguments”, this is what the functions will need to work (typically some data, and some indications on what to do with those data).

We’ll come back to that in more details in a little bit. For the moment, let’s just see how we can use them. For example, as previously sneakily shown, we can calculate the square root of a number with the function “sqrt()”.

sqrt(vec)
414214 1.732051 3.162278      NaN      NaN
Warning message:
In sqrt(vec) : NaNs produced

You will notice that R in its almighty greatness will refuse to give you a result for the square root of a negative number). It won’t have any problem however if you let him know that you are willing to lose yourself in the complexity of complex number. To do that, we can simply use the function “as.complex()” to indicate that our numbers are considered in the realm of complex numbers.

sqrt(as.complex(vec))
[1] 1.414214+0.0000000i 1.732051+0.0000000i 3.162278+0.0000000i 0.000000+2.2360680i
[5] 0.000000+0.5744563i

Those functions are incredibly useful since they can do things as simple as computing an exponent, and as complex as conducting analyses and then automatically create maps and plot graphs of your results, all the while saving all those information in a format that will be reusable later! Basically, if you can think of it, you can create (or find) a function that will do it!

If you ever need help on a function, or want to know more about how to use it, the arguments you can/have to specify, just ask a question. A question mark followed by the name of the function you’re interested in. Let’s try it with the square root function:

? sqrt

Or simply request for “help()”

help("sqrt")

If you are not sure about the name of the function, or are looking for a general subject, you can use a double question mark, followed by the keyword related to your question:

?? box

“Help! I need somebody!”

Help! Not just anybody

One of the most useful, confusing, important and frustrating elements of R’s functions is their corresponding help files. You can pretty much categorize them in 2 categories: the helpful ones and the non-helpful help files… Typically, this document is going to be organized in 6 to 10 sections. Whether this file is going to save your day or to be as useful as a gummy crowbar depends on how well these sections are completed. But don’t fret, either way, once you understand their purpose, it becomes much easier to find out what you’re looking for, and maybe you’ll realize that the power was in you all along. The most common sections include:

Description

A short description of the purpose of the function.

Usage

The general way to call the function, including the list of the necessary and optional arguments that can be used.

Arguments

A description of each argument (i.e. input) that the function relies on.

Details

A more thorough presentation of the inner workings of the function.

Value

The kind of output that is returned, and its content.

Note

Notes (I don’t know what I was expecting…).

Author(s)

Names of the wizards that created this beautiful work of art.

References

Standing on the shoulder of giants you get a better view, let’s thanks these giants.

See also

A list of other related (and potentially relevant) functions.

Examples

Pieces of code that can be directly copy-pasted in the R console to show a practical application of the function.

And if you need more help, we put some help in your help! Check it out:

help("help")

INTRODUCTION

Once upon a time…

MANAGING OUR DATA!

Learn how to view, export and import your data!

DATA!

R allows you to handle all kind of data. Here a short description of what you can use and define.

CONCLUSION

Ready to crank it to 11?