Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add challenges and explanations on classes and modes for #55 #273

Merged
merged 6 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 75 additions & 6 deletions episodes/01-r-basics.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
39 changes: 37 additions & 2 deletions episodes/03-basics-factors-dataframes.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
of 29 variables (columns). Double-clicking on the name of the object will open
a view of the data in a new tab.

![RStudio data frame view]("fig/rstudio_dataframeview.png")

Check warning on line 188 in episodes/03-basics-factors-dataframes.Rmd

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[image missing alt-text]: "fig/rstudio_dataframeview.png"

## Summarizing, subsetting, and determining the structure of a data frame.

Expand Down Expand Up @@ -234,6 +234,43 @@
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
Expand Down Expand Up @@ -776,12 +813,12 @@
choose **From Excel...** (notice there are several other options you can
explore).

![RStudio import menu]("fig/rstudio_import_menu.png")

Check warning on line 816 in episodes/03-basics-factors-dataframes.Rmd

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[image missing alt-text]: "fig/rstudio_import_menu.png"

Next, under **File/Url:** click the <KBD>Browse</KBD> button and navigate to the **Ecoli\_metadata.xlsx** file located at `/home/dcuser/dc_sample_data/R`.
You should now see a preview of the data to be imported:

![RStudio import screen]("fig/rstudio_import_screen.png")

Check warning on line 821 in episodes/03-basics-factors-dataframes.Rmd

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[image missing alt-text]: "fig/rstudio_import_screen.png"

Notice that you have the option to change the data type of each variable by
clicking arrow (drop-down menu) next to each column title. Under **Import
Expand Down Expand Up @@ -861,5 +898,3 @@
- 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

::::::::::::::::::::::::::::::::::::::::::::::::::


Loading