diff --git a/01_the_r_environment.qmd b/01_the_r_environment.qmd
index b52911b..37b0eea 100644
--- a/01_the_r_environment.qmd
+++ b/01_the_r_environment.qmd
@@ -1,21 +1,21 @@
# The R *Environment*
**R** is an environment for statistical modeling and graphics. That it is an
-*environment* instead of a *package* is one of the main reasons **R** is
+*environment* instead of a *package* is one of the main reasons why **R** is
distinct from other statistical programs. A package only gives us a fixed set of
tools. An environment allows us to modify, combine and even create tools to suit
our specific needs.
## Why becoming a useR?
-**R** is gratis and it runs on Windows, MacOS, and several Unix platforms. With **R** you can start with a data set that looks like this:
+**R** is gratis and it runs on Windows, MacOS, and several Unix platforms. With **R** you can start with a data set like this:
```{r loading messy flower data}
#| echo: false
flower_df <- read.table("data_files/flower.csv", header = TRUE, sep = ",")
head(flower_df, 5)
```
-And, in 8 lines of code or less, you can get a plot that looks like this:
+and, in 8 lines of code or less, make a plot like this:
```{r height by nitrogen boxplots}
#| echo: false
boxplot(
@@ -31,21 +31,21 @@ boxplot(
You can also find many powerful tools to fit statistical models
(both bayesian and frequentist), including:
-- Linear and Generalized Linear Models
+- Generalized linear models (including linear regression)
- Survival analysis
- Time series analysis
-- Random and Mixed Effects Models (aka hierarchical models, aka multilevel
+- Random and Mixed effects models (aka hierarchical models, aka multilevel
models)
-- Classification and Clustering
-- Sample Size and Power Calculations
-- Multivariable analysis (e.g., Factor Analysis, Principal Component Analysis, and Structural Equations Modeling)
+- Classification and clustering
+- Sample size and power calculations
+- Multivariable analysis (e.g., factor analysis, principal component analysis, and structural equations modeling)
-Even better, new tools become available in **R** all the time. As with other open source programming languages, everyone can examine and contribute to **R**'s code. Users constantly publish their own code "packages" (ready-to-use tools), which expand **R**'s base capabilities. As of March 2019, users have contributed over 13,700 packages to Comprehensive **R** Archive Network (CRAN)., many of which perform complex statistical routines that are not (and may never be) available in other statistical software systems.
+Even better, new tools become available in **R** all the time. As with other open source programming languages, everyone can examine and contribute to **R**'s code. Users constantly publish their own code "packages" (ready-to-use tools), which expand **R**'s base capabilities. As of March 2019, users have contributed over 13,700 packages to Comprehensive **R** Archive Network (CRAN), many of which perform complex statistical routines that are not (and may never be) available in other statistical software systems.
You can choose your own way of using **R**. In Windows, there are several ways to use **R**. The standard **R** graphical unit interface (GUI) allows you to point and click to do many basic tasks. Another GUI is [**R** Commander](https://socialsciences.mcmaster.ca/jfox/Misc/Rcmdr/), developed by
-John Fox at McMaster University. **R** Commander displays the underlying **R** code for each analysis to help the user learn the programming language. [Tinn-**R**](http://sourceforge.net/projects/tinn-r/) is another GUI from Jose Claudio Faria.
+John Fox at McMaster University. **R** Commander displays the underlying **R** code for each analysis to help the user learn the programming language. [Tinn-**R**](http://sourceforge.net/projects/tinn-r/) is another GUI from Jose Claudio Faria. These GUIs are friendly and easy to grasp if you're a beginner. But to use all of **R**'s capabilities you will need to do more than point and click.
-These GUIs are friendly and easy to grasp if you're a beginner. But to use all of **R**'s capabilities you will need to do more than point and click. A more complete way of using **R** is through an integrated development environment (IDE), which, in short, helps you code. The most popular IDE for **R** is [**R**Studio](https://www.posit.com), which organizes the
+A more complete way of using **R** is through an integrated development environment (IDE), which, in short, helps you code. The most popular IDE for **R** is [**R**Studio](https://www.posit.com), which organizes the
user's screen into panes that display scripts, objects, graphics, and
the **R** console.
@@ -63,10 +63,15 @@ original analysis and to investigate other approaches.
## Why Isn't Everyone a Use**R**?
-Maybe because many users of statistics only learn how to use one statistical software, often the one taught in their first statistics course. In the past, **R** was almost never this first language, but nowadays many schools are teaching how to use it.
+Maybe because many users of statistics only learn how to use one statistical software, often the one taught in their first statistics course. In the past, **R** was almost never this first language (although nowadays more schools are teaching how to use it).
-And even if people have used **R**, they can struggle to get comfortable and
-productive with it, especially if they have little coding experience. Typing commands explicitly is more difficult than pointing and clicking. Also, each package has its own rules to learn. You can find a lot of good help for popular packages written by professional developers, but not so much for smaller packages written by other common users. Worst of all, some of the messages **R** displays if you make a mistake are uninformative, so figuring out what went wrong can be difficult.
+Besides, even if people have used **R**, they can struggle to get comfortable
+and productive with it, especially if they have little coding experience. Typing
+commands explicitly is more difficult than pointing and clicking. Also, each
+package has its own rules to learn. You can find a lot of good help for popular
+packages written by professional developers, but not so much for smaller
+packages written by other common users. Worst of all, some of the messages **R**
+displays if you make a mistake are uninformative, so fixing the problem can be difficult.
Don't get frustrated! You don't have to be an expert programmer to use
**R**. The benefits are worth spending some time up front.
@@ -74,7 +79,7 @@ Don't get frustrated! You don't have to be an expert programmer to use
## Suggestions for Learning **R**
- Learn interactively! Retype and experiment with lots of sample code; you won't break it. These notes contain several code examples and you can find many more online.
-- Don't worry about getting errors. Experienced **R** users
+- Don't worry about getting errors. Even experienced **R** users
make errors all the time. Besides, you can learn a lot from error
messages.
- Ask other **R** users for help.
@@ -93,7 +98,7 @@ Don't get frustrated! You don't have to be an expert programmer to use
Education at UCLA
- : John Fox's home page
- : Examples of code to perform same task
- in each language
+ in SAS and R.
## How to get **R**
diff --git a/02_getting_started_with_r.qmd b/02_getting_started_with_r.qmd
index 97e3556..8c7ab9c 100644
--- a/02_getting_started_with_r.qmd
+++ b/02_getting_started_with_r.qmd
@@ -2,9 +2,9 @@
## The RStudio interface
-The first screen we see in R should look like this:
+The first screen we see in **R** should look like this:
-[![The first screen we see when we open RStudio](./images/rstudio_first_screen.jpg)](./images/rstudio_first_screen.jpg)
+[![The first screen we see when we open **R**Studio](./images/rstudio_first_screen.jpg)](./images/rstudio_first_screen.jpg)
Here, there are three main window panes. The "Console" window is where we type the code to tell the computer to do stuff.
@@ -141,12 +141,12 @@ In this context, both `<-` and `=` are acting as "assignment operators" because
Now the environment window shows that there is an object called `my_object`, which we can access at anytime by typing its name.
+[![The environment pane with an object](./images/environment_with_object.jpg)](./images/environment_with_object.jpg)
+
```{r call my_object}
my_object
```
-[![The environment pane with an object](./images/environment_with_object.jpg)](./images/environment_with_object.jpg)
-
`my_object` has a number as its value, so we can do mathematical operations with it.
```{r}
@@ -163,7 +163,7 @@ a
b
```
-Be careful with names. If we try to reuse a name, **R** will overwrite the previous object without notifying us in any way.
+Be careful: if we try to reuse a name, **R** will overwrite the previous object without notifying us in any way.
```{r overwriting object}
c <- 2
@@ -189,18 +189,18 @@ We can use almost any name we want, but there are a few rules:
+ Names can *not* include white spaces.
+ If a name starts with a period `.`, it can *not* be followed by a digit.
+ Names can *not* start with a number or an underscore `_`.
-+ Names are case-sensitive (`age`, `Age` and `AGE` are three different variables).
-+ Reserved words can *not* be used as variables (`TRUE`, `FALSE`, `NULL`, `if`, ...).
++ Names are case-sensitive (`age`, `Age` and `AGE` are three different objects).
++ Reserved words can *not* be used as names (`TRUE`, `FALSE`, `NULL`, `if`, ...).
There are also a few suggestions that will save you many hours of frustration:
+ Avoid giving your object the same name as a built-in function.
-+ If you need to create objects with multiple words in their name, separate them with an underscore (`my_value`) or a dot (`my.value`), or capitalize the different words (`MyValue`). I like the underscore format (called [snake case](https://en.wikipedia.org/wiki/Snake_case) naming). You can use whichever one you want. Just be consistent.
++ If you need to create objects with multiple words in their name, separate them with an underscore (`my_value`) or a dot (`my.value`), or capitalize the different words (`MyValue`). Choose whichever format you like most. Just be consistent.
+ Use informative names. It is quick and easy to use names like `x` or `my_value`. But your code will be easier and faster to understand if your objects have names that illustrate what you want to do with them. Your colleagues and your future self will really appreciate it.
## Using functions
-**R** has many built-in functions for anything from basic math to text manipulation to advanced statistical models. In **R**, functions are objects that store commands that we can apply to an input. For example, we can round a number, calculate its square root, its factorial, or its natural logarithm. To use a function, we must write the name of a function followed by the input we want it to use inside a pair of round brackets.
+**R** has many built-in functions for anything from basic math to text manipulation to advanced statistical models. **R** thinks of functions as objects that store commands that we can apply to an input. For example, we can round a number, calculate its square root, its factorial, or its natural logarithm. To use a function, we must write the name of a function followed by the input we want it to use inside a pair of round brackets.
```{r simple mathematical functions}
#| results: hide
@@ -217,7 +217,7 @@ round(2.1415, 0)
round(2.1415, 2)
```
-The order of the arguments is important (what happens if we try to run `round(2, 2.1415)`?) Fortunately, arguments have names that you can use to specify which data to use in each case. So, we can write
+The order of these arguments is important (what happens if we try to run `round(2, 2.1415)`?) Fortunately, arguments have names that you can use to specify which data to use in each case. So, we can write
```{r naming arguments}
round(x = 2.1415, digits = 3)
@@ -229,7 +229,7 @@ Even better, we can pass arguments in any order as long as we name them all.
round(digits = 3, x = 2.1415)
```
-But if we use the wrong name in a function, we will likely get an error message.
+Note that using the wrong name in a function will likely yield an error message.
```{r}
#| error: true
@@ -242,11 +242,11 @@ A quick way to check what arguments we can use is to invoke `args()`, which take
args(round)
```
-The output shows that `round()` has two arguments: `x` (the number we want to round) and `digits`. It also shows that `digits` is already set to zero. This means that **R** will use `0` as the default value. Arguments with default values are optional because we can choose to modify them or not.
+The output shows that `round()` has two arguments: `x` (the number we want to round) and `digits`. It also shows that `digits` is already set to zero. This means that **R** will use `0` as the default value. We can omit arguments with default values and **R** will automatically fill them for us in the background.
I encourage you to name all the arguments in the function, or at least the ones after the first argument. It is hard to remember all the arguments that can go into more complicated functions. And it is even harder to remember them in the correct order. Accidentally switching unnamed arguments can produce wrong results without us knowing. Naming the arguments will prevent errors and clarify the function's behavior.
-We can also run functions inside other functions. This works because **R** first runs the innermost function and then uses the result in the outermost function to get the final result.
+We can also run functions inside other functions. This works because **R** first runs the innermost function and then uses the result in the outermost function.
```{r nested function}
sqrt(round(16.483, digits = 0))
@@ -272,22 +272,16 @@ A shorter way of writing this is to use `?` before the name of the function.
After you run the code, the help page is displayed in the "Help" tab in the "Files-Plots-Packages" pane (usually in the bottom right of **R**Studio).
-As a novice user, help pages may seem arcane---probably because they aim for shortness and use technical jargon. But this short jargon makes (most of) the explanations precise, so we can use the information we need without having to read the entire document. Also, all help pages are organized similarly, so we don't have to relearn how to navigate them. With a bit of practice, you will be able to find exactly what you need in mere seconds.
+As a novice user, help pages may seem arcane---probably because they aim for shortness and use technical terminology. But this short jargon makes (most of) the explanations precise, so we can use the information we need without having to read the entire document. Also, all help pages are organized similarly, so we don't have to relearn how to navigate them. With a bit of practice, you will be able to find exactly what you need in mere seconds.
The first line of the help document displays the name of the function and the package that contains the function. Other sections are:
+ **Description**: a short description of the function.
-
+ **Usage**: names the arguments associated with the function and possible default values.
-
+ **Arguments**: expounds each argument and what they do.
-
+ **Details**: a more detailed description of what the function does and how it does it.
-
+ **Value**: if applicable, gives the type and structure of the object returned by the function or the operator.
-
+ **See Also**: leads to other help pages with similar or related content.
-
+ **Examples**: code examples on how to use the function. We can see how they work if we copy and paste them into the console. We can also access examples at any time by using the `example()` function (e.g., `example("round")`).
`help()` is useful if we know the name of the function. But if all we remember is a key word in the name, we can search through **R**'s help system using `help.search()`
diff --git a/docs/01_the_r_environment.html b/docs/01_the_r_environment.html
index 3a5d87d..70bd2c5 100644
--- a/docs/01_the_r_environment.html
+++ b/docs/01_the_r_environment.html
@@ -2,7 +2,7 @@
-
+
@@ -49,7 +49,13 @@
"collapse-after": 3,
"panel-placement": "start",
"type": "textbox",
- "limit": 20,
+ "limit": 50,
+ "keyboard-shortcut": [
+ "f",
+ "/",
+ "s"
+ ],
+ "show-item-context": false,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
@@ -58,6 +64,7 @@
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
+ "search-text-placeholder": "",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit",
"search-label": "Search"
@@ -73,12 +80,12 @@