diff --git a/episodes/01-r-basics.Rmd b/episodes/01-r-basics.Rmd index 7d706976..1f7c9378 100644 --- a/episodes/01-r-basics.Rmd +++ b/episodes/01-r-basics.Rmd @@ -252,16 +252,42 @@ longer exists. Error: object 'gene_name' not found ``` -## Understanding object data types (modes) +## Understanding object data types (classes and modes) -In R, **every object has two properties**: +In R, **every object has several properties**: - **Length**: How many distinct values are held in that object - **Mode**: What is the classification (type) of that object. +- **Class**: A property assigned to an object that determines how a function + will operate on it. We will get to the "length" property later in the lesson. The **"mode" property** -**corresponds to the type of data an object represents**. The most common modes -you will encounter in R are: +**corresponds to the type of data an object represents** and the **"class" property determines how functions will work with that object.** + + +::::::::::::::::::::::::::::::::::::::::: callout + +## Tip: Classess vs. modes + +The difference between modes and classes is a bit **confusing** and the subject of +several [online discussions](https://stackoverflow.com/questions/35445112/what-is-the-difference-between-mode-and-class-in-r). +Often, these terms are used interchangeably. Do you really need to know +the difference? + +Well, perhaps. This section is important for you to have a better understanding +of how R works and how to write usable code. However, you might not come across +a situation where the difference is crucial while you are taking your first steps +in learning R. However, the overarching concept—**that objects in R have these properties and that you can use functions to check or change them**—is very important! + +In this lesson we will mostly stick to **mode** but we will throw in a few +examples of the `class()` and `typeof()` so you can see some examples of where +it may make a difference. + +:::::::::::::::::::::::::::::::::::::::::::::::::: + + + +The most common modes you will encounter in R are: | Mode (abbreviation) | Type of data | | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -276,9 +302,9 @@ Data types are familiar in many programming languages, but also in natural language where we refer to them as the parts of speech, e.g. nouns, verbs, adverbs, etc. Once you know if a word - perhaps an unfamiliar one - is a noun, you can probably guess you can count it and make it plural if there is more than -one (e.g. 1 [Tuatara](https://en.wikipedia.org/wiki/Tuatara), or 2 Tuataras). If +one (e.g., 1 [Tuatara](https://en.wikipedia.org/wiki/Tuatara), or 2 Tuataras). If something is a adjective, you can usually change it into an adverb by adding -"-ly" (e.g. [jejune](https://www.merriam-webster.com/dictionary/jejune) vs. +"-ly" (e.g., [jejune](https://www.merriam-webster.com/dictionary/jejune) vs. jejunely). Depending on the context, you may need to decide if a word is in one category or another (e.g "cut" may be a noun when it's on your finger, or a verb when you are preparing vegetables). These concepts have important analogies when @@ -325,6 +351,44 @@ mode(pilot) :::::::::::::::::::::::::::::::::::::::::::::::::: +::::::::::::::::::::::::::::::::::::::: challenge + + +## Exercise: Create objects and check their class using "class" + +Using the objects created in the previous challenge, use the `class()` function +to check their classes. + +::::::::::::::: solution + +## Solution + +```{r, echo=FALSE, purl=FALSE} +chromosome_name <- 'chr02' +od_600_value <- 0.47 +chr_position <- '1001701' +spock <- TRUE + +``` + + +```{r, purl=FALSE} +class(chromosome_name) +class(od_600_value) +class(chr_position) +class(spock) +``` + +```{r, purl=FALSE} +class(pilot) +``` + +::::::::::::::::::::::::: + +:::::::::::::::::::::::::::::::::::::::::::::::::: + +Notice that in the two challenges, `mode()` and `class()` return the same results. This time... + Notice from the solution that even if a series of numbers is given as a value R will consider them to be in the "character" mode if they are enclosed as single or double quotes. Also, notice that you cannot take a string of alphanumeric @@ -340,6 +404,11 @@ pilot <- "Earhart" mode(pilot) ``` +```{r, purl=FALSE} +pilot <- "Earhart" +typeof(pilot) +``` + ## Mathematical and functional operations on objects Once an object exists (which by definition also means it has a mode), R can diff --git a/episodes/03-basics-factors-dataframes.Rmd b/episodes/03-basics-factors-dataframes.Rmd index 049560f2..dff912a6 100644 --- a/episodes/03-basics-factors-dataframes.Rmd +++ b/episodes/03-basics-factors-dataframes.Rmd @@ -234,6 +234,43 @@ Ok, thats a lot up unpack! Some things to notice. by the object mode (e.g. chr, int, etc.). Notice that before each variable name there is a `$` - this will be important later. + + + ::::::::::::::::::::::::::::::::::::::: challenge + + ## Exercise: Revisiting modes and classess + + Remeber when we said mode and class are sometimes different? If you do, here + is a chance to check. What happens when you try the following? + + 1. `mode(variants)` + 2. `class(variants)` + + ::::::::::::::: solution + + ## Solution + + + + ```{r, purl=FALSE} + mode(variants) + ``` + + + + ```{r, purl=FALSE} + class(variants) + ``` + + This result makes sense because `mode()` (which deals with how an object is stored) + tells us that `variants` is treated as a **list** in R. A data frame is in some sense a "fancy" list. + However, data fames do have some specific properties beyond that of a basic list, so they have their own + class (**data.frame**), which is important for functions (and programmers) to know. + ::::::::::::::::::::::::: + + :::::::::::::::::::::::::::::::::::::::::::::::::: + + ## Introducing Factors Factors are the final major data structure we will introduce in our R genomics @@ -861,5 +898,3 @@ write.csv(Ecoli_metadata, file = "exercise_solution.csv") - Base R has many useful functions for manipulating your data, but all of R's capabilities are greatly enhanced by software packages developed by the community :::::::::::::::::::::::::::::::::::::::::::::::::: - -