Skip to content

Commit 24f093d

Browse files
committed
Merge branch 'main' of github.com:datacarpentry/genomics-r-intro
2 parents 9d6ec6f + 2202d7c commit 24f093d

File tree

2 files changed

+112
-8
lines changed

2 files changed

+112
-8
lines changed

episodes/01-r-basics.Rmd

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,42 @@ longer exists.
252252
Error: object 'gene_name' not found
253253
```
254254

255-
## Understanding object data types (modes)
255+
## Understanding object data types (classes and modes)
256256

257-
In R, **every object has two properties**:
257+
In R, **every object has several properties**:
258258

259259
- **Length**: How many distinct values are held in that object
260260
- **Mode**: What is the classification (type) of that object.
261+
- **Class**: A property assigned to an object that determines how a function
262+
will operate on it.
261263

262264
We will get to the "length" property later in the lesson. The **"mode" property**
263-
**corresponds to the type of data an object represents**. The most common modes
264-
you will encounter in R are:
265+
**corresponds to the type of data an object represents** and the **"class" property determines how functions will work with that object.**
266+
267+
268+
::::::::::::::::::::::::::::::::::::::::: callout
269+
270+
## Tip: Classess vs. modes
271+
272+
The difference between modes and classes is a bit **confusing** and the subject of
273+
several [online discussions](https://stackoverflow.com/questions/35445112/what-is-the-difference-between-mode-and-class-in-r).
274+
Often, these terms are used interchangeably. Do you really need to know
275+
the difference?
276+
277+
Well, perhaps. This section is important for you to have a better understanding
278+
of how R works and how to write usable code. However, you might not come across
279+
a situation where the difference is crucial while you are taking your first steps
280+
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!
281+
282+
In this lesson we will mostly stick to **mode** but we will throw in a few
283+
examples of the `class()` and `typeof()` so you can see some examples of where
284+
it may make a difference.
285+
286+
::::::::::::::::::::::::::::::::::::::::::::::::::
287+
288+
289+
290+
The most common modes you will encounter in R are:
265291

266292
| Mode (abbreviation) | Type of data |
267293
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
@@ -276,9 +302,9 @@ Data types are familiar in many programming languages, but also in natural
276302
language where we refer to them as the parts of speech, e.g. nouns, verbs,
277303
adverbs, etc. Once you know if a word - perhaps an unfamiliar one - is a noun,
278304
you can probably guess you can count it and make it plural if there is more than
279-
one (e.g. 1 [Tuatara](https://en.wikipedia.org/wiki/Tuatara), or 2 Tuataras). If
305+
one (e.g., 1 [Tuatara](https://en.wikipedia.org/wiki/Tuatara), or 2 Tuataras). If
280306
something is a adjective, you can usually change it into an adverb by adding
281-
"-ly" (e.g. [jejune](https://www.merriam-webster.com/dictionary/jejune) vs.
307+
"-ly" (e.g., [jejune](https://www.merriam-webster.com/dictionary/jejune) vs.
282308
jejunely). Depending on the context, you may need to decide if a word is in one
283309
category or another (e.g "cut" may be a noun when it's on your finger, or a verb
284310
when you are preparing vegetables). These concepts have important analogies when
@@ -325,6 +351,44 @@ mode(pilot)
325351

326352
::::::::::::::::::::::::::::::::::::::::::::::::::
327353

354+
::::::::::::::::::::::::::::::::::::::: challenge
355+
356+
357+
## Exercise: Create objects and check their class using "class"
358+
359+
Using the objects created in the previous challenge, use the `class()` function
360+
to check their classes.
361+
362+
::::::::::::::: solution
363+
364+
## Solution
365+
366+
```{r, echo=FALSE, purl=FALSE}
367+
chromosome_name <- 'chr02'
368+
od_600_value <- 0.47
369+
chr_position <- '1001701'
370+
spock <- TRUE
371+
372+
```
373+
374+
375+
```{r, purl=FALSE}
376+
class(chromosome_name)
377+
class(od_600_value)
378+
class(chr_position)
379+
class(spock)
380+
```
381+
382+
```{r, purl=FALSE}
383+
class(pilot)
384+
```
385+
386+
:::::::::::::::::::::::::
387+
388+
::::::::::::::::::::::::::::::::::::::::::::::::::
389+
390+
Notice that in the two challenges, `mode()` and `class()` return the same results. This time...
391+
328392
Notice from the solution that even if a series of numbers is given as a value
329393
R will consider them to be in the "character" mode if they are enclosed as
330394
single or double quotes. Also, notice that you cannot take a string of alphanumeric
@@ -340,6 +404,11 @@ pilot <- "Earhart"
340404
mode(pilot)
341405
```
342406

407+
```{r, purl=FALSE}
408+
pilot <- "Earhart"
409+
typeof(pilot)
410+
```
411+
343412
## Mathematical and functional operations on objects
344413

345414
Once an object exists (which by definition also means it has a mode), R can

episodes/03-basics-factors-dataframes.Rmd

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,43 @@ Ok, thats a lot up unpack! Some things to notice.
234234
by the object mode (e.g. chr, int, etc.). Notice that before each
235235
variable name there is a `$` - this will be important later.
236236

237+
238+
239+
::::::::::::::::::::::::::::::::::::::: challenge
240+
241+
## Exercise: Revisiting modes and classess
242+
243+
Remeber when we said mode and class are sometimes different? If you do, here
244+
is a chance to check. What happens when you try the following?
245+
246+
1. `mode(variants)`
247+
2. `class(variants)`
248+
249+
::::::::::::::: solution
250+
251+
## Solution
252+
253+
254+
255+
```{r, purl=FALSE}
256+
mode(variants)
257+
```
258+
259+
260+
261+
```{r, purl=FALSE}
262+
class(variants)
263+
```
264+
265+
This result makes sense because `mode()` (which deals with how an object is stored)
266+
tells us that `variants` is treated as a **list** in R. A data frame is in some sense a "fancy" list.
267+
However, data fames do have some specific properties beyond that of a basic list, so they have their own
268+
class (**data.frame**), which is important for functions (and programmers) to know.
269+
:::::::::::::::::::::::::
270+
271+
::::::::::::::::::::::::::::::::::::::::::::::::::
272+
273+
237274
## Introducing Factors
238275

239276
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")
861898
- 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
862899

863900
::::::::::::::::::::::::::::::::::::::::::::::::::
864-
865-

0 commit comments

Comments
 (0)