Skip to content

Commit

Permalink
Typo correction in file expressing-yourself.Rmd
Browse files Browse the repository at this point in the history
The discussion of the code in lines 236-243 was a little confusing with x and y so I proposed changing it to a and b. Not sure if that was just an error that crept in while rewriting and fiddling around with the sentence or a conscious decision from you.
  • Loading branch information
shoili committed Nov 18, 2015
1 parent 05223eb commit 83864ba
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions expressing-yourself.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ The pipe is a powerful tool, but it's not the only tool at your disposal, and it
## Duplication
As you become a better R programming, you'll learn more techniques for reducing various types of duplication. This allows you to do more with less, and allows you to express yourself more clearly by taking advantage of powerful programming constructs.
As you become a better R programmer, you'll learn more techniques for reducing various types of duplication. This allows you to do more with less, and allows you to express yourself more clearly by taking advantage of powerful programming constructs.

Two main tools for reducing duplication are functions and for-loops. You tend to use for-loops less often in R than in other programming languages because R is a functional programming language. That means that you can extract out common patterns of for loops and put them in a function.

Expand All @@ -243,7 +243,7 @@ df$d <- (df$d - min(df$d, na.rm = TRUE)) /
(max(df$d, na.rm = TRUE) - min(df$d, na.rm = TRUE))
```
You might be able to puzzle out that this rescales each column to 0--1. Did you spot the mistake? I made an error when updating the code for `df$y`, and I forgot to change an `x` to a `y`. Extracting repeated code out into a function is a good idea because it helps make your code more understandable (because you can name the operation), and it prevents you from making this sort of update error.
You might be able to puzzle out that this rescales each column to 0--1. Did you spot the mistake? I made an error when updating the code for `df$b`, and I forgot to change an `a` to a `b`. Extracting repeated code out into a function is a good idea because it helps make your code more understandable (because you can name the operation), and it prevents you from making this sort of update error.
To write a function you need to first analyse the operation. How many inputs does it have?
Expand Down Expand Up @@ -275,7 +275,7 @@ rescale01 <- function(x) {
}
```

Always make sure you code works on a simple test case before creating the function!
Always make sure your code works on a simple test case before creating the function!

Now we can use that to simplify our original example:

Expand Down Expand Up @@ -304,7 +304,7 @@ for (i in 1:ncol(df)) {
medians
```

If you do this a lot, you'd probably pull make a function for it:
If you do this a lot, you should probably make a function for it:

```{r}
col_medians <- function(df) {
Expand Down

0 comments on commit 83864ba

Please sign in to comment.