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

Correct format for 01-supp-data-structures lesson #101

Merged
merged 3 commits into from
May 14, 2015
Merged
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
61 changes: 29 additions & 32 deletions 01-supp-data-structures.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ subtitle: Data types and structures
minutes: 45
---

> ## Objectives {.objectives}
> Expose learners to the different data types in R
> ## Learning Objectives {.objectives}
>
> Learn how to create vectors of different types
>
> Be able to check the type of vector
>
> Learn about missing data and other special values
>
> Getting familiar with the different data structures (lists, matrices, data frames)
> * Expose learners to the different data types in R
> * Learn how to create vectors of different types
> * Be able to check the type of vector
> * Learn about missing data and other special values
> * Getting familiar with the different data structures (lists, matrices, data frames)

### Understanding basic data types in R
### Understanding Basic Data Types in R

To make the best of the R language, you'll need a strong understanding of the
basic data types and data structures and how to operate on those.
Expand Down Expand Up @@ -86,8 +83,7 @@ workhorse of R. Technically, vectors can be one of two types:

although the term "vector" most commonly refers to the atomic types not to lists.


#### The different vector modes
### The Different Vector Modes

A vector is a collection of elements that are most commonly of mode `character`,
`logical`, `integer` or `numeric`.
Expand Down Expand Up @@ -132,7 +128,7 @@ While using quoted text will create a vector of mode `character`:
z <- c("Sarah", "Tracy", "Jon")
```

#### Examining vectors
### Examining Vectors

The functions `typeof()`, `length()`, `class()` and `str()` provide useful
information about your vectors and R objects in general.
Expand All @@ -148,7 +144,7 @@ str(z)
>
> Do you see a property that's common to all these vectors above?

#### Adding elements
### Adding Elements

The function `c()` (for combine) can also be used to add elements to a vector.

Expand All @@ -159,7 +155,7 @@ z <- c("Greg", z)
z
```

#### Vectors from a sequence of numbers
### Vectors from a Sequence of Numbers

You can create vectors as a sequence of numbers.

Expand All @@ -169,7 +165,7 @@ seq(10)
seq(from = 1, to = 10, by = 0.1)
```

#### Missing data
### Missing Data

R supports missing data in vectors. They are represented as `NA` (Not Available)
and can be used for all the vector types covered in this lesson:
Expand All @@ -194,8 +190,7 @@ anyNA(x)
anyNA(y)
```

#### Other special values

### Other Special Values

`Inf` is infinity. You can have either positive or negative infinity.

Expand All @@ -209,7 +204,7 @@ anyNA(y)
0/0
```

#### What happens when you mix types inside a vector?
### What Happens When You Mix Types Inside a Vector?

R will create a resulting vector with a mode that can most easily accommodate
all the elements it contains. This conversion between modes of storage is called
Expand All @@ -231,7 +226,7 @@ as.numeric("1")
as.character(1:2)
```

#### Object attributes
### Objects Attributes

Objects can have __attributes__. Attributes are part of the object. These include:

Expand Down Expand Up @@ -338,7 +333,7 @@ Elements are indexed by double brackets. Single brackets will still return
a(nother) list.


### Data frame
### Data Frame

A data frame is a very important data type in R. It's pretty much the *de facto*
data structure for most tabular data and what we use for statistics.
Expand All @@ -358,23 +353,25 @@ Some additional information on data frames:
* Find the number of rows and columns with `nrow(dat)` and `ncol(dat)`, respectively.
* Rownames are usually 1, 2, ..., n.

#### Creating data frames by hand
### Creating Data Frames by Hand

To create data frames by hand:

```{r}
dat <- data.frame(id = letters[1:10], x = 1:10, y = 11:20)
dat
```

#### Useful data frame functions

* `head()` - shown first 6 rows
* `tail()` - show last 6 rows
* `dim()` - returns the dimensions
* `nrow()` - number of rows
* `ncol()` - number of columns
* `str()` - structure of each column
* `names()` - shows the `names` attribute for a data frame, which gives the
column names.
> ## Useful data frame functions {.callout}
>
> * `head()` - shown first 6 rows
> * `tail()` - show last 6 rows
> * `dim()` - returns the dimensions
> * `nrow()` - number of rows
> * `ncol()` - number of columns
> * `str()` - structure of each column
> * `names()` - shows the `names` attribute for a data frame, which gives the
>column names.

See that it is actually a special list:

Expand Down