## Nominal variables in R

Imagine that you surveyed several members of your family on what they think of each other. Originally, you store the names of all participants in a character vector in R, but because the information you obtained could cause disputes within the family, you decide to replace these names with arbitrarily chosen numbers. This way, no one will be able to see who said what in the survey and (hopefully) no disputes will arise.

To implement this correctly, you will have to perform an additional step. If you were to leave this numeric vector as is, R would treat it as a vector of numbers. But multiplying your grandfather and your father does not make much sense, does it? To make R treat these values as **nominal variables** instead of numbers, you should use the `factor()`

function. This function takes the name of the vector to transform and converts its elements into nominal factor variables.

In general, telling R precisely what type of variable you are working with is a good practice that can save you time and prevent careless mistakes. If you're ever unsure of what type of variable you're dealing with, you can use the `class()`

function to find out.

### Instructions

- Assign a vector containing the numbers
`2, 3, 5, 7, 11, 13, 17`

to the variable`participants1`

- Check the class of
`participants1`

- Transform this numeric vector to a factor vector and assign it to
`participants2`

- Check the class of
`participants2`

```
# Create a numeric vector with the identifiers of the participants of your survey
participants1 <- ___
# Check what type of values R thinks the vector consists of
class(participants1)
# Transform the numeric vector to a factor vector
participants2 <- ___
# Check what type of values R thinks the vector consists of now
class(participants2)
```

```
# Create a numeric vector with the identifiers of the participants of your survey
participants1 <- c(2, 3, 5, 7, 11, 13, 17)
# Check what type of values R thinks the vector consists of
class(participants1)
# Transform the numeric vector to a factor vector
participants2 <- factor(participants1)
# Check what type of values R thinks the vector consists of now
class(participants2)
```

```
test_object("participants1",
undefined_msg = "Did you assign the numeric vector to the variable <code>participants1</code>?",
incorrect_msg = "It looks like you did not correctly assign the numeric vector to the variable <code>participants1</code>.")
test_object("participants2", eval = FALSE, undefined_msg = "Did you assign the factor vector to the variable <code>participants2</code>?")
test_function("factor", not_called_msg = "Use <code>factor()</code> function to convert the numeric vector to a factor vector. Type <code>?factor</code> in the console to get the help file.")
test_object("participants2", incorrect_msg = "It looks like <code>participants2</code> is not correct. Did you convert the numeric vector into a factor vector?")
success_msg("Good job! Press the \"Next Exercise\" button to move on.")
```

## Ordinal variables in R

The `factor()`

function also allows you to assign an order to the nominal variables, thus making them ordinal variables. This is done by setting the `order`

parameter to `TRUE`

and by assigning a vector with the desired level hierarchy to the argument `levels`

. Since we do not want to force you to rank order your family members, we'll illustrate this with a different example.

Consider the categorical variable `temperature_vector`

with the categories `"Low"`

, `"Medium"`

and `"High"`

. Here it's obvious that `"Medium"`

stands above `"Low"`

, and that `"High"`

stands above `"Medium"`

. Let's use R to create this rank ordering among weather observations.

### Instructions

Click "Submit Answer" to run the code and see how R constructs and prints ordinal variables.

```
# Create a vector of temperature observations
temperature_vector <- c("High", "Low", "High", "Low", "Medium")
# Specify that they are ordinal variables with the given levels
factor_temperature_vector <- factor(temperature_vector, order = TRUE,
levels = c("Low", "Medium", "High"))
# Print the result to the console
factor_temperature_vector
```

```
# Create a list of temperature observations
temperature_vector <- c("High", "Low", "High", "Low", "Medium")
# Specify that they are ordinal variables with the given levels
factor_temperature_vector <- factor(temperature_vector, order = TRUE,
levels = c("Low", "Medium", "High"))
# Print the result to the console
factor_temperature_vector
```

```
success_msg("Great! Make sure to take a look at <code>factor_temperature_vector</code> before moving on to the next exercise.")
```

- Just click the "Submit Answer" button to run the code.

## Interval and ratio variables in R

R has no special way of dealing with interval and ratio variables. In fact, it's not necessary because you can just use plain old numeric values.

### Instructions

Review the examples in the script. The first example creates an interval variable called `longitudes`

containing a vector of longitudes. The second example creates a ratio variable called `chronos`

containing the times it takes for an athlete to run 100 meters.

Click "Submit Answer" once you understand how interval and ratio variables are expressed in R.

```
# Create an interval variable called longitudes
longitudes <- c(10, 20, 30, 40)
# Create a ratio variable called chronos
chronos <- c(10.60, 10.12, 9.58, 11.1)
```

```
# Create an interval variable called longitudes
longitudes <- c(10, 20, 30, 40)
# Create a ratio variable called chronos
chronos <- c(10.60, 10.12, 9.58, 11.1)
```

```
success_msg("Great! Press the \"Next Exercise\" button to move on.")
```

Just click "Submit Answer" once you understand how interval and ratio variables are expressed in R.

<p>For additional information on the Stevens paper: <a href="http://www.mpopa.ro/statistica_licenta/Stevens_Measurement.pdf">"On the Theory of Scales of Measurement"</a>.</p>