## 1.2 Solutions

### 1.2.1 Exercise 1

Create a new file called `assignment1.R`

in your `PUBL0055`

folder and write all the solutions in it.

In RStudio, go to the menu and select **File** > **New File** > **R Script**

Make sure to clear the environment and set the working directory.

```
rm(list = ls())
setwd("~/PUBL0055")
```

Go to the menu and select **File** > **Save** and name it `assignment1.R`

.

### 1.2.2 Exercise 2

Calculate the square root of `1369`

using the `sqrt()`

function.

`sqrt(1369)`

`[1] 37`

### 1.2.3 Exercise 3

Square the number `13`

using the `^`

operator.

`13^2`

`[1] 169`

### 1.2.4 Exercise 4

What is the result of summing all numbers from `1`

to `100`

?

```
numbers_1_to_100 <- seq(1, 100, 1)
result <- sum(numbers_1_to_100)
result
```

`[1] 5050`

The result is 5050

### 1.2.5 Exercise 5

Use the `names()`

function to display the variable names of the `longley`

dataset.

`names(longley)`

```
[1] "GNP.deflator" "GNP" "Unemployed" "Armed.Forces"
[5] "Population" "Year" "Employed"
```

### 1.2.6 Exercise 6

Use square brackets to access column `4`

of the dataset.

`longley[, 4]`

```
[1] 159.0 145.6 161.6 165.0 309.9 359.4 354.7 335.0 304.8 285.7 279.8
[12] 263.7 255.2 251.4 257.2 282.7
```

### 1.2.7 Exercise 7

Use the dollar sign to access column `4`

of the dataset.

To solve this task we need to know the name of the fourth variable because using the dollar sign we can access variables in our dataset using their names. Recall, that `names(longley)`

returns all variable names in our dataset. You can then count and select the name of the fourth variable or you can subset `names(lognley)`

using square brackets like so:

`names(longley)[4]`

`[1] "Armed.Forces"`

`longley$Armed.Forces`

```
[1] 159.0 145.6 161.6 165.0 309.9 359.4 354.7 335.0 304.8 285.7 279.8
[12] 263.7 255.2 251.4 257.2 282.7
```

### 1.2.8 Exercise 8

Access the two cells from row `4`

, column `1`

and row `6`

, column `3`

.

`longley[4, 1]`

`[1] 89.5`

`longley[6, 3]`

`[1] 193.2`

### 1.2.9 Exercise 9

Using the `longley`

data produce a line plot with GNP on the y-axis and population on the x-axis.

```
plot( GNP ~ Population, data = longley,
type = "l",
bty = "n",
main = "Relationship of GNP and Population Size")
```

### 1.2.10 Exercise 10

Use the help function to find out how to label the y-axis “Wealth” and the x-axis “Population”.

`help(plot)`

You can also use the `?`

to get help:

`?plot`

The help page tells us that the argument `xlab`

lets us set a label for the x-axis and `ylab`

for the y-axis.

```
plot(GNP ~ Population, data = longley,
bty = "n",
pch = 16,
main = "Relationship of GNP and Population Size",
xlab = "Population",
ylab = "Wealth")
```

We’re using a couple of additional arguments (i.e. `bty`

and `pch`

) to make the plot look a bit nicer. These and other arguments not handled by the `plot`

function itself are described in the help for **graphical parameters**.

`help("graphical parameters")`

or you could just use `?par`

`?par`