R crash course: Your first code

In the last tutorial, we downloaded R and set up our workplace. Now, we’re all set to start typing some basic commands. Let’s get right to it!

Copy the following code into a new script in RStudio, send it to the console line by line and see what happens.

If you run the code above correctly, the variables x, y, w and q should be added to your environment. If so, run the following code (the Run button or Ctrl+ENTER, you remember) and look at what R returns as output. For the beginning, I strongly recommend copying the code into an RStudio script and then run it line by line:

If everything works, R should show you a result looking like this:

Congratulations! You used R as a pocket calculator! There are many functions that help you to do maths with R.

For example:

Feel free to play around with those new basic skills for a while!


Code and comments

As you may have noticed, I am using “#” whenever I write something that is not code. This is called a comment. It’s very useful to add explanations to your code so future you or other people know what your code does or why you coded something the way you did. If I don’t want R to actually perform a line of code, I can use the hashtag or double apostrophes (‘..’), too.

Let R do the following step by step and compare the results:

R only defines the variable z to be equal to the value 7, since the rest of the lines are commented out via hashtag and apostrophes. If you ask R what z is, it tells you that it’s seven. But if you want R to show you the value of a or b, it doesn’t find the variables. Because you never told R to define them. If you forget to mark a whole sentence as a comment, like we did in the last line, R starts to look for a variable that is named like the first word of your sentence. If it can’t find any in the environment, it will return an error message.


R: Getting Help

When learning to code, you’ll encounter a lot of new things. Don’t worry if you forget how exactly they work. Noone can remember it all. That’s why it is all the more important to know where to get help in R!

Of course, you can find a lot of things by simply googling buzz words, the function names or the error messages you get. Googling is a valuable skill for programming, since chances are someone has asked your question before. Never underestimate the power of the internet!

But R offers some helpful features you can use directly in the console as well.

Copy the code above and see what happens in RStudio when you run it.

Bildschirmfoto 2015-12-22 um 18.31.20

Similar to the picture on the right, RStudio will automatically open the R Documentation on the function “sum” that belongs to the package named “base”. Typing “?sum” or “?’sum'” would do the same, by the way. Try it!

The R Help gives you a description of what a function does, how to use it, what arguments to put between the brackets and some examples using the function. This can be very helpful, although the details may be hard to understand at first.  But you’ll get there.

To get help on basic stuff, R forums like Quick-R are always worth looking at. For example, if you want to create a specific plot, it’s always good to know its english name and then look for examples using R on the web.

So far, you’ve learnt how to set up R, how to use it for basic calculations and how to get help if you’re stuck. In the next chapter, we’ll get to know some basic functions. You’ll also get a feeling for how R stores data.

Comments ( 2 )

  1. Zweiter journocode-Beitrag zum R-Tutorial: Your first code | Datentäter
    […] Den Artikel findet ihr auch auf der journocode-website. […]
  2. R crash course: Vectors – Journocode
    […] look at a few useful functions that can help you analyze vectors. Remember to use the help functions or the internet if you don’t understand a […]

Leave a reply

Your email address will not be published.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.