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.
x <- 6 # here we tell R that from now on the variable x stands for the value 6. # We can use the arrow or the equal sign for this assignment. # I prefer the arrow. y = 3 # this is the same as writing "y <- 3" # you also can give your values a longer name: myage <- 23 # actually you don't have to work with numeric variables all the time! Try this: w <- "journocode" q <- "is the best" # note the quotations marks that tell R "Watch out! This is a name/a sentences!" # These are called character strings
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:
x y x+y 2*x y/x # and also paste(w,q) # w+q doesn't work here because w and q are not numeric but characters.
If everything works, R should show you a result looking like this:
> x  6 > y  3 > x+y  9 > 2*x  12 > x/y  2 > paste(w,q)  "journocode is the best" # what, really? Oh, stop it you! We'll blush!
Congratulations! You used R as a pocket calculator! There are many functions that help you to do maths with R.
sqrt(9) # returns the square root of 9 log(2) # calculates the logarithm of 2 sum(4,5) # does the same as 4+5 abs(-8) # gives you the absolute value of -8
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:
z <- 7 # a <- 4 'b <- 3' z a b # this is a comment 'this is a comment, too' but this is what R does not like at all
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.
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.