Skip to content

Commit b3978b1

Browse files
committed
Typos
1 parent 27b4c22 commit b3978b1

19 files changed

+61
-63
lines changed

1_03_building_scripts.Rmd

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
## Introduction
44

5-
We've seen that using variables is useful because it enables us to break down a problem into a series of simpler steps. Howevre, so far we've only been working in the Console. If we want to reuse a calculation when we're working like this, we have to change a variable or two and then evaluate the expressions that do the job of solving an equation, make a graph, whatever. We also have to do all of this in the correct order, or things will not work as intended.
5+
We've seen that using variables is useful because it enables us to break down a problem into a series of simpler steps. However, so far we've only been working in the Console. If we want to reuse a calculation when we're working like this, we have to change a variable or two and then evaluate the expressions that do the job of solving an equation, make a graph, whatever. We also have to do all of this in the correct order, or things will not work as intended.
66

7-
We can see that working in the Console is not going to be practical most of the time. So what should we do? The answer is: put our sequence of R expressions into a text file, called a __script__. Calling it a script makes it sound a bit fancy and clever---"I spent all day debugging my script". It is not. It is a boring text file that could be opend up in something like Notepad.exe. We just call it a script to signify the fact that the text contained in the file is a series of instructions telling our computer to do something.
7+
We can see that working in the Console is not going to be practical most of the time. So what should we do? The answer is: put our sequence of R expressions into a text file, called a __script__. Calling it a script makes it sound a bit fancy and clever---"I spent all day debugging my script". It is not. It is a boring text file that could be opened up in something like Notepad.exe. We just call it a script to signify the fact that the text contained in the file is a series of instructions telling our computer to do something.
88

99
```{block, type="warning"}
1010
Working directly at the Console is the simplest way to use R, but we do not recommend working this way unless you only need to do something very simple that involves a handful of steps. For more complicated activities you should always store your instructions in a script.
@@ -18,9 +18,9 @@ To open a new script in RStudio navigate to `File > New File > R Script`. This w
1818
knitr::include_graphics("images/RStudio-4-pane.png")
1919
```
2020

21-
When we work with a script we type the required sequence of R expressions into the Editor pane, **not directly into the Console**. This is important---if we mix and match mistakes happen. The worst of these is that we write a script that seems to work, only to find it is broken when we open it up and use it again later. This usually happens because we typed something into the Console that is needed to make the whole script run when we were preparing it, but then forget to put it into the script. Just don't swirch between using the Console and the Editor to avoid this.
21+
When we work with a script we type the required sequence of R expressions into the Editor pane, **not directly into the Console**. This is important---if we mix and match mistakes happen. The worst of these is that we write a script that seems to work, only to find it is broken when we open it up and use it again later. This usually happens because we typed something into the Console that is needed to make the whole script run when we were preparing it, but then forget to put it into the script. Just don't switch between using the Console and the Editor to avoid this.
2222

23-
The easiest way to appreciate the benefits of using a script is to work with one. Here are a few lines of R code to copy-paste this into the new Editor pane...
23+
The easiest way to appreciate the benefits of using a script is to work with one. Here are a few lines of R code to copy-paste into the new Editor pane...
2424

2525
```{r, eval=FALSE}
2626
a <- 1
@@ -57,7 +57,7 @@ At this point we just want to emphasise that __you should always use comments in
5757

5858
## Running scripts in RStudio
5959

60-
The whole point of writing a script is ultimatley to run it. The phrase "run our code" is shorthand for "send a number of R expressions to the R interpreter to be read and evaluated". The latter is tedious to write (and read) over and over again, so we will just write "run your/my/our code". We could run the code in the above script by copying and pasting it into the Console, but this is inefficient. Instead of relying on cut and paste, RStudio gives us different ways to run our code:
60+
The whole point of writing a script is ultimately to run it. The phrase "run our code" is shorthand for "send a number of R expressions to the R interpreter to be read and evaluated". The latter is tedious to write (and read) over and over again, so we will just write "run your/my/our code". We could run the code in the above script by copying and pasting it into the Console, but this is inefficient. Instead of relying on cut and paste, RStudio gives us different ways to run our code:
6161

6262
* There is a `Run` button at the top right of the Editor pane. As we might imagine, clicking on this will run some code. If we haven't highlighted anything in the Editor, this runs whichever line the cursor is at, i.e. it runs just that one line. If we had highlighted a region inside the Editor, this button will run all of that in sequence.
6363

1_04_functions.Rmd

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ What about the bits inside the parentheses? These are called the __arguments__ o
1616

1717
In the simple example above, we used the `round` function with two arguments. Each of these was supplied as a name-value pair, separated by a comma. When working with arguments, name-value pairs occur either side of the equals (`=`) sign, with the __name__ of the argument on the left hand side and the __value__ it takes on the right hand side (notice that the syntax highlighter we used to make this website helpfully colours the argument names differently from the values). The name serves to identify which argument we are working with, and the value is the thing that controls what that argument does in the function.
1818

19-
We refer to the process of associating argument names and values as "supplying the arguments" of the function (sometimes we also say "setting the arguments"). Notice the similarly between supplying function arguments and the assignment operation discussed in the last topic. The difference here is that name-value pairs are associated with the `=` symbol. This association is also temporary: it only lasts as long as it takes for the function to do whatever it does.
19+
We refer to the process of associating argument names and values as "supplying the arguments" of the function (sometimes we also say "setting the arguments"). Notice the similarity between supplying function arguments and the assignment operation discussed in the last topic. The difference here is that name-value pairs are associated with the `=` symbol. This association is also temporary: it only lasts as long as it takes for the function to do whatever it does.
2020

2121
```{block, type="warning"}
2222
#### Use `=` to assign arguments
2323
2424
__Do not__ use the assignment operator ` <- ` inside the parentheses when working with functions. This is a "trust us" situation: you will end up in all kinds of difficulty if you do this.
2525
```
2626

27-
The names of the arguments that we are allowed to use are typically determined for us by the function. That is, we are not free to choose whatever name we like. We say "typically", because R is a very flexible language and so there are certain exceptions to this simple rule of thumb. For now it is simpler to think of the names as constrained by the particular function we're using. The arguments control the behaviour of a function. Our job as users is to set the values of the these to get the behaviour we want. By now it is now probably fairly obvious what is going to happen when we used the `round` function like this at the Console:
27+
The names of the arguments that we are allowed to use are typically determined for us by the function. That is, we are not free to choose whatever name we like. We say "typically", because R is a very flexible language and so there are certain exceptions to this simple rule of thumb. For now it is simpler to think of the names as constrained by the particular function we're using. The arguments control the behaviour of a function. Our job as users is to set the values of these to get the behaviour we want. By now it is now probably fairly obvious what is going to happen when we used the `round` function like this at the Console:
2828

2929
```{r}
3030
round(x = 3.141593, digits = 2)
@@ -60,7 +60,7 @@ We said at the beginning of this section that a function may optionally __return
6060
```{r}
6161
2 * round(x = 2.64, digits = 0)
6262
```
63-
Here the The R interpreter first evaluates the function call, and then multiplies the value it returns by 2. If we want to reuse this value we have to assign the result of function call, for example:
63+
Here the R interpreter first evaluates the function call, and then multiplies the value it returns by 2. If we want to reuse this value we have to assign the result of function call, for example:
6464
```{r}
6565
roundnum <- 2 * round(x = 2.64, digits = 0)
6666
```
@@ -81,13 +81,13 @@ If we had meant to round the value of `myvar` so that we can use this new value
8181
myvar <- 3.7
8282
myvar <- round(x = myvar, digits = 0)
8383
```
84-
In this example, we just overwrote the old value, but we could just as easily have created a new variable. The reason this is worth pointing out is that new users sometimes assume certain types of functions will alter their arguments. Specifically, when working with functions manipulate something called a `data.frame`, there is a tendency to assume that the function changes the `data.frame` argument. It will not. If wee want to make use of the changes, rather than just see them printed to the Console, we need to assign the results. We can do this by creating a new variable or overwriting the old one. We'll gain first hand experience of this in the Data Wrangling block.
84+
In this example, we just overwrote the old value, but we could just as easily have created a new variable. The reason this is worth pointing out is that new users sometimes assume certain types of functions will alter their arguments. Specifically, when working with functions manipulate something called a `data.frame`, there is a tendency to assume that the function changes the `data.frame` argument. It will not. If we want to make use of the changes, rather than just see them printed to the Console, we need to assign the results. We can do this by creating a new variable or overwriting the old one. We'll gain first hand experience of this in the Data Wrangling block.
8585

8686
Remember, functions do not have side effects! New R users sometimes forget this and create all kinds of headaches for themselves. Don't be that person.
8787

8888
## Combining functions {#combining-functions}
8989

90-
Up until now we have not tried to do anything very complicated in our examples. Using R to actually get useful work almost always involves multiple steps, very often facilitated a number of different functions. There is more than one way to do this. Here's a simple example that takes an approach we already know about:
90+
Up until now we have not tried to do anything very complicated in our examples. Using R to actually get useful work almost always involves multiple steps, very often facilitated by a number of different functions. There is more than one way to do this. Here's a simple example that takes an approach we already know about:
9191
```{r}
9292
myvar <- sqrt(x = 10)
9393
round(x = myvar, digits = 0)

1_05_numeric_vectors.Rmd

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Introduction
44

5-
The term "data structure" computer science jargon for a particular way of organising data on our computers, so that it can be accessed easily and efficiently. Computer languages use many different kinds of data structures, but fortunately, we only need to learn about a couple of relatively simple ones to use R for data analysis. In fact, in this book only two kinds of data structure really matter: "vectors" and "data frames". We'll learn how to work with data frames in the next section of the book.
5+
The term "data structure" is computer science jargon for a particular way of organising data on our computers, so that it can be accessed easily and efficiently. Computer languages use many different kinds of data structures, but fortunately, we only need to learn about a couple of relatively simple ones to use R for data analysis. In fact, in this book only two kinds of data structure really matter: "vectors" and "data frames". We'll learn how to work with data frames in the next section of the book.
66

77
The next three chapters will consider vectors. This chapter has two goals. First, we want to learn the basics of how to work with vectors. For example, we'll see how "vectorised operations" may be used to express a repeated calculation. Second, we'll learn how to construct and use __numeric vectors__ to perform various calculations. Keep in mind that although we're going to focus on numeric vectors, many of the principles we learn here can be applied to the other kinds of vectors considered later.
88

@@ -184,7 +184,7 @@ rep(rep(seqvec, each = 2), times = 3)
184184

185185
## Vectorised operations
186186

187-
All the simple arithmetic operators (e.g. `+` and `-`) and many mathematical functions are __vectorised__ in R . What this this means is that when we use a vectorised function it operates on vectors on an element-by-element basis. Take a look at this example to see what we mean by this:
187+
All the simple arithmetic operators (e.g. `+` and `-`) and many mathematical functions are __vectorised__ in R . What this means is that when we use a vectorised function it operates on vectors on an element-by-element basis. Take a look at this example to see what we mean by this:
188188
```{r}
189189
# make a simple sequence
190190
x <- 1:10

1_06_more_vectors.Rmd

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ rep(l_vec, times = 2)
5353
```
5454
Hopefully nothing about that output is surprising by this point.
5555

56-
So why are logical vectors useful? Their allow us to represent the results of questions such as, "is x greater than y" or "is x equal to y". The results of such comparisons may then be used to carry out various kinds of subsetting operations based.
56+
So why are logical vectors useful? Their allow us to represent the results of questions such as, "is x greater than y" or "is x equal to y". The results of such comparisons may then be used to carry out various kinds of subsetting operations.
5757

5858
Let's first look at how we use logical vectors to evaluate comparisons. Before we can do that though we need to introduce __relational operators__. These sound fancy, but they are very simple: we use relational operators to evaluate the relative value of vector elements. Six are available in R:
5959

@@ -75,7 +75,7 @@ Now, if we need to evaluate and represent a question like, "is x greater than th
7575
```{r}
7676
x > y
7777
```
78-
The `x > y` expression produces a logical vector, with `TRUE` values associated with elements in `x` are less than `y`, and `FALSE` otherwise. In this example, x is less than y until we reach the value of 15 in each sequence. Notice that that relational operators are vectorised: the work on an element by element basis. They wouldn't be much use if this were not the case.
78+
The `x > y` expression produces a logical vector, with `TRUE` values associated with elements in `x` are less than `y`, and `FALSE` otherwise. In this example, x is less than y until we reach the value of 15 in each sequence. Notice that relational operators are vectorised: they work on an element by element basis. They wouldn't be much use if this were not the case.
7979

8080
What does the `==` operator do? It compares the elements of two vectors to determine if they are exactly equal:
8181
```{r}

0 commit comments

Comments
 (0)