Using single numbers or data points will only get us so far. If we want to analyse data sets or do anything with more than one number, it would be very helpful to have tools that grouped numbers. Fortunately, R gives us several of these which will help to solve different problems. This article looks at one of these tools – ‘Lists’. We will learn what a list is, how we can use them in functions, and methods that help us to get more use out of them.

Lists are our simplest way to group numbers. They are simply a list of values – whether that is numbers, text, variables or even another list. Let’s put our ‘goals for’ and ‘goals against’ from our team’s first 9 games into a list:

` ````
GF = c(0,0,3,2,1,5,3,1,2)
GA = c(2,0,2,2,3,2,3,2,4)
print(GF)
## [1] 0 0 3 2 1 5 3 1 2
```

` ````
print(GA)
## [1] 2 0 2 2 3 2 3 2 4
```

Accessing numbers in lists is easy. Let’s take the numbers from our first game.

We do this by calling the list name, and adding square brackets with a number on the end. We will get the value held in the positon of the number.

This order starts at 1 – so for the first game, we will put 1. The second, we put 2, the third, we put 3 and so on.

` ````
print(GF[1])
## [1] 0
```

` ````
print(GF[2])
## [1] 2
```

Looks like we lost 2-0. Let’s make sure:

` ````
GF[1] - GA[1]
## [1] -2
```

Yup, a loss and -2 goal difference.

Let’s iterate through this list to get our goal difference and also our points for the season so far.

` ````
#Define variables for points and goal difference
#Set goals for and against
P <- 0
GD <- 0
GF <- c(0,0,3,2,1,5,3,1,2)
GA <- c(2,0,2,2,3,2,3,2,4)
#We have 9 games, so we iterate with a range of 9 in a for loop
for (i in seq(1:9)){
#If we score more, give us 3 points and add the goal difference
if(GF[i] > GA[i]){
P <- P +3
GD <- GD + (GF[i] - GA[i])
}else if(GF[i] == GA[i]){
P <- P + 1
}else{
GD <- GD + (GF[i] - GA[i])
}
}
print(P)
## [1] 9
```

` ````
print(GD)
## [1] -3
```

In those 9 games, we received 9 points and had a -3 GD. We can change the lists to be any length, and change the number in the ‘seqence’ to calculate this over any number of games!

‘Seqence’ is a function that generates a series of numbers between two points. By default, it starts at 1 and goes to whatever number we give it. Let’s take a look at some examples:

` ````
#One argument in range()
#Give me the list of 10 numbers, starting at 0
seq(0,9,1)
## [1] 0 1 2 3 4 5 6 7 8 9
```

` ````
#Two arguments in range()
#Give me all numbers between these two
seq(5,10,1)
## [1] 5 6 7 8 9 10
```

` ````
#Three arguments in range()
#Give me all numbers between the first two, in steps of the third number
seq(0,10,2)
## [1] 0 2 4 6 8 10
```

We then used this range as our counter for how many times we used the for loop. Keep this in mind for future loops!

Selecting a number from a list is easy. Just pop the index (the location of the number) in square brackets after the list name.

Select multiple with a colon between two numbers – including the first of them, but not the last.

` ````
Players <- c("Didek","Bascin","Smacer","Boras","Finnon")
#Select the 4th player - remember that the count starts at 0!
print(Players[4])
## [1] "Boras"
```

` ````
#Select the middle 3 players (Bascin, Smacer and Boras)
print(Players[2:4])
## [1] "Bascin" "Smacer" "Boras"
```

A method is a function that belongs to a data type. It is used to manipulate or utilise this data type.

For lists, this might involve adding something to the end, alphaetising our list or finding the location of something within it.

A method is used by calling the name of the variable, then the method and its arguments – you can find a few below:

` ````
GF <- c(0,0,3,2,1,5,3,1,2)
GA <- c(2,0,2,2,3,2,3,2,4)
#Lets add a new score to our goals for and goals against lists
#The append() method will help us here
GF <- append(GF, 3)
GA <- append(GA, 1)
#Show the scores, 3-1 should be the final column
print(GF)
## [1] 0 0 3 2 1 5 3 1 2 3
```

` ````
print(GA)
## [1] 2 0 2 2 3 2 3 2 4 1
```

` ````
GF <- c(0, 0, 3, 2, 1, 5, 3, 1, 2, 3)
GA <- c(2, 0, 2, 2, 3, 2, 3, 2, 4, 1)
#I want to reverse the scores, to see the 5 most recent ones
#Reverse() will make this light work
GF <- rev(GF)
GA <- rev(GA)
print(GF[1:5])
## [1] 3 2 1 3 5
```

` ````
print(GA[1:5])
## [1] 1 4 2 3 2
```

There are plenty more methods for you to learn about on your journey learning R – I’m sure you’ll find lots more on the pages here, but a quick Google will take you to many more.

Lists are a simple way to store lots of numbers under one variable. This is very useful for keeping records of things, or for running formulas that take more than one number.

We have learned that they are created with c and brackets c(), can be navigated to find individual datapoints or ‘slices’ of then.

We also learned about methods – functions that act on a particular class. For lists, we saw them easily add more data points, or reverse the order of them.

There are more ways to store multiple data points – more on those later.

- Numbers, Variables & Strings
- Comparisons & Logic
- If Statements
- For Loops
- While Loops
- Lists
- Packages
- Creating Functions
- 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!