Skip to content

Commit

Permalink
clarify callout box on type conversion for sliced rows of data frames…
Browse files Browse the repository at this point in the history
…; add info to instructor notes
  • Loading branch information
lmweber committed Jan 23, 2017
1 parent 2ce0da9 commit 0f2ea4c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
29 changes: 11 additions & 18 deletions _episodes_rmd/01-starting-with-data.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -288,24 +288,6 @@ patient_1 <- dat[1, ]
max(patient_1)
```
> ## Forcing Conversion
>
> The code above may give you an error in some R installations,
> since R does not automatically convert a sliced row of a `data.frame` to a vector.
> (Confusingly, sliced columns are automatically converted.)
> If this happens, you can use the `as.numeric` command to convert the row of data to a numeric vector:
>
> `patient_1 <- as.numeric(dat[1, ])`
>
> `max(patient_1)`
>
> You can also check the `class` of each object:
>
> `class(dat[1, ])`
>
> `class(as.numeric(dat[1, ]))`
{: .callout}
We don't actually need to store the row in a variable of its own.
Instead, we can combine the selection and the function call:
Expand All @@ -327,6 +309,17 @@ median(dat[, 7])
sd(dat[, 7])
```
> ## Forcing Conversion
>
> Note that R may return an error when you attempt to perform similar calculations on
> sliced *rows* of data frames. This is because some functions in R automatically convert
> the object type to a numeric vector, while others do not (e.g. `max(dat[1, ])` works as
> expected, while `mean(dat[1, ])` returns an error). You can fix this by including an
> explicit call to `as.numeric()`, e.g. `mean(as.numeric(dat[1, ]))`. By contrast,
> calculations on sliced *columns* always work as expected, since columns of data frames
> are already defined as vectors.
{: .callout}
What if we need the maximum inflammation for all patients, or the average for each day?
As the diagram below shows, we want to perform the operation across a margin of the data frame:
Expand Down
9 changes: 9 additions & 0 deletions _extras/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ line and this should resolve the issue.
* Provide shortcut for the assignment operator (`<-`) (RStudio: Alt+- on
Windows/Linux; Option+- on Mac).

* When performing operations on sliced rows of data frames, be aware that some
functions in R automatically convert the object type to a numeric vector, while
others do not. For example, `max(dat[1, ])` works as expected, while `mean(dat[1, ])`
returns an error. You can fix this by including an explicit call to `as.numeric()`,
for example `mean(as.numeric(dat[1, ]))`. This issue is also mentioned in a callout
box in the lesson materials, since it is unexpected and can create confusion when
simple examples fail (by contrast, operations on sliced columns of data frames always
work as expected, since columns of data frames are already vectors).

## [Addressing Data]({{ page.root }}/10-supp-addressing-data/)

* Note that the data frame `dat` is not the same set of data as in other lessons.
Expand Down

0 comments on commit 0f2ea4c

Please sign in to comment.