R Basics

1. Numbers, Variables & Strings

Football is full of numbers, names, lists, teams and a million other 'things' that make up our understanding of what is happening.

In the same way, R has lots of different ways of classifying things that it can understand. For example, it uses numbers to count, 'strings' for names and even has ways to group these things – like we would need for a league. This introductory post takes a look at a couple of these data types.

Numbers


As its simplest, R is a calculator:

        
  1+1

  ## [1] 2
        
        
  10*2

  ## [1] 20
        
        
  20/2

  ## [1] 10
        
        
  # ** = to the power of
  2**3

  ## [1] 8
        

Variables


While a calculator is incredibly useful, we can give these numbers a name and a placeholder so that they are a bit more tangible and applicable to a problem. For example, if I want to keep track of our top scorer's shots and goals:

        
  Ronney_Goals <- 15
  Ronney_Shots <- 63

I could now calculate Ronney's conversion rate! Dividing goals by shots, I can see how many shots it takes him to score.

        
  Ronney_Conversion = Ronney_Goals/Ronney_Shots
  Ronney_Conversion

  ## [1] 0.2380952
        

Strings


Now that R has some interesting information on Ronney, it wants to share it with the world. As we can see above, sharing a number by itself doesn't tell us a great deal. However, a string of text can give us a bit of context.

Surround a piece of text in quotation marks (be consistent with single or double quotes) to create a string:

        
    "Ronney is truly a great player, his conversion rate speaks for itself."

    ## [1] "Ronney is truly a great player, his conversion rate speaks for itself."
        

...and let's use the print() and paste0() commands to add the evidence to our commentator's opinion.

        
    print(paste0("His conversion rate of ", Ronney_Conversion, " is sublime"))

    ## [1] "His conversion rate of 0.238095238095238 is sublime"
        

That is a bit specific, Merse, but I appreciate the information! Let's take a look at those two commands:

print() - R, please print everything that I put into the brackets.

paste0() - R, combine this segments of text and numbers

Once you have a grasp of the concepts above, you should aim to move on to our other tutorials for R. Here, you'll learn about cleaning and analysing data in R, as well as visualisation of your insights.

If you have any questions, recommendations or requests, please get in touch!

This is a R conversion of a tutorial series by FC Python. I take no credit for the idea and have their blessing to make this conversion. All text is a direct copy unless changes were relevant. Please follow them on twitter and if you have a desire to learn Python then they are a fantastic resource!

Next Lessons...


  1. Numbers, Variables & Strings
  2. Comparisons & Logic
  3. If Statements
  4. For Loops
  5. While Loops
  6. Lists
  7. Packages
  8. Creating Functions
  9. Random Numbers with Expected Goals

This is a R conversion of a tutorial by FC Python. I take no credit for the idea and have their blessing to make this conversion. All text is a direct copy unless changes were relevant. Please follow them on twitter and if you have a desire to learn Python then they are a fantastic resource!