1.4 Basic R Syntax and Data Types

Learning Outcomes

By the end of this tutorial, you should:

Morton FC

Variables and Assignment

In programming, variables are containers in which we store data values and store the results of calculations or operations on existing data.[1]

Assignment operators allow you to place values into variables.

Assignment Operators

There are three assignment operators in R: <-, -> and =.

Performing Operations with Variables}

The <- assignment operator can also be used to perform arithmetical calculations:

# Define two variables, x and y
x <- 10
y <- 3

# Perform some arithmetic operations on those variables
sum <- x + y
difference <- x - y
product <- x * y       # the product is the outcome when we multiply
quotient <- x / y      # the quotient is the outcome when we divide

# Print results to the console
print(sum)
[1] 13
print(difference)
[1] 7
print(product)
[1] 30
print(quotient)
[1] 3.333333

Updating Variables

The <- operator can also be used to update variables, for example to increment a variable:

turn <- 0           # set 'turn' to zero
turn <- turn + 1    # increment 'turn' by 1
print(turn)         # print updated value of 'turn'
[1] 1

Data Types in R

When dealing with sport data, we are often faced with different types of data. Some data might be numerical, like a match outcome.[2] Some might be text, like a team name.[3]

In R, you can use the function to get a quick overview of the different types of data in your dataset.

There are six basic types of data that R recognises. By clearly defining which type of data each variable holds, you will find it much easier to work with the data later on.

Character

Character data types are used to represent text data, including individual characters, strings, and words. They are always enclosed in double or single quotes. Examples include “hello”, ‘R programming’.

Vectors and Basic Vector Operations

Creating Vectors

We can use the function to create a vector by combining elements. Remember, all elements in a vector have to be of the same :

# create a numeric vector with five elements
numeric_vector <- c(1, 2, 3, 4, 5) 

# create a character vector with three elements
character_vector <- c("playerOne", "playerTwo", "playerThree")

# create a logical vector with three elements
logical_vector <- c(TRUE, FALSE, TRUE)  

We can perform element-wise arithmetic and logical operations on vectors:

%—————————————————————–

Finally, we can use logical to filter or subset vectors:

even_numbers <- numeric_vector[numeric_vector %% 2 == 0]
long_strings <- character_vector[nchar(character_vector) > 5] 

Activity: Creating Vectors and Performing Simple Calculations

Task 1: Create a Vector

Write an R script to create a vector named that contains the integers from 1 to 10.

player_id <- 1:10
print(player_id)
 [1]  1  2  3  4  5  6  7  8  9 10

Task 2: Vector Operations

Create two vectors and . should contain numbers from 1 to 5, and contain numbers from 6 to 10. Then add, subtract, multiply, and divide these vectors.

a <- 1:5
b <- 6:10
print(a + b)
[1]  7  9 11 13 15
print(a - b)
[1] -5 -5 -5 -5 -5
print(a * b)
[1]  6 14 24 36 50
print(a / b)
[1] 0.1666667 0.2857143 0.3750000 0.4444444 0.5000000

Task 6: Named Vectors

Create a named vector that contains the number of apples, oranges, bananas, and grapes. Set the values to 10, 5, 8, and 12, respectively.

fruit_basket <- c(apples = 10, oranges = 5, bananas = 8, grapes = 12)
print(fruit_basket)
 apples oranges bananas  grapes 
     10       5       8      12 

Task 7: Manipulating Named Vectors

Using the vector from the previous exercise, add 5 to the number of apples, subtract 2 from the number of oranges, and print the updated vector.

fruit_basket["apples"] <- fruit_basket["apples"] + 5
fruit_basket["oranges"] <- fruit_basket["oranges"] - 2
print(fruit_basket)
 apples oranges bananas  grapes 
     15       3       8      12 

References

1.
Finley MI, Pleket HW. The olympic games: The first thousand years. Courier Corporation; 2012.
2.
Elling A, Hovden J, Knoppers A. Gender diversity in european sport governance. Routledge; 2018.
3.
Toohey K, Veal AJ. The olympic games: A social science perspective. Cabi; 2007.