Skip to content

Commit

Permalink
typos
Browse files Browse the repository at this point in the history
  • Loading branch information
b-rodrigues committed Jun 10, 2022
1 parent 63ea694 commit f529558
Show file tree
Hide file tree
Showing 132 changed files with 3,827 additions and 2,809 deletions.
57 changes: 40 additions & 17 deletions 02-data_types.Rmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Objects, types and useful R functions to get started
# Objects, their classes and types, and useful R functions to get you started

All objects in R have a given *type*. You already know most of them, as these types are also used
in mathematics. Integers, floating point numbers, or floats, matrices, etc, are all objects you
in mathematics. Integers, floating point numbers (floats), matrices, etc, are all objects you
are already familiar with. But R has other, maybe lesser known data types (that you can find in a
lot of other programming languages) that you need to become familiar with. But first, we need to
learn how to assign a value to a variable. This can be done in two ways:
Expand Down Expand Up @@ -72,9 +72,23 @@ There is also `is.numeric()` which tests whether a number is of the `numeric` cl
is.numeric(a)
```

These functions are very useful, there is one for any of the supported types in R. Later, we are going
to learn about the `{purrr}` package, which is a very powerful package for functional programming. This
package includes further such functions.
It is also possible to create an integer using `L`:

```{r}
a <- 5L
class(a)
```

Another way to convert this integer back to a floating point number is to use `as.double()` instead of
as numeric:

```{r}
class(as.double(a))
```

The functions prefixed with `is.*` and `as.*` are quite useful, there is one for any of the supported types in R, such
as `as/is.character()`, `as/is.factor()`, etc...

## The `character` class

Expand All @@ -94,7 +108,11 @@ To convert something to a character you can use the `as.character()` function:
a <- 4.392
class(a)
```

Now let's convert it:

```{r}
class(as.character(a))
```

Expand Down Expand Up @@ -142,7 +160,7 @@ called `{lubridate}`. We are going to go over this package in Chapter 4.

## The `logical` class

This class is the result of logical comparisons, for example, if you type:
This is the class of predicates, expressions that evaluate to *true* or *false*. For example, if you type:

```{r}
4 > 3
Expand Down Expand Up @@ -172,7 +190,7 @@ k <- 4 > 3
```

The `!` operator indicates negation, so the above expression could be translated as *is k not TRUE?*.
There are other such operators, namely `&, &&, |, ||`. `&` means *and* and `|` stands for *or*.
There are other operators for boolean algebra, namely `&, &&, |, ||`. `&` means *and* and `|` stands for *or*.
You might be wondering what the difference between `&` and `&&` is? Or between `|` and `||`? `&` and
`|` work on vectors, doing pairwise comparisons:

Expand Down Expand Up @@ -215,7 +233,7 @@ all(one & two)
`any()` checks whether any of the vector's elements are `TRUE` and `all()` checks if all elements of the vector are
`TRUE`.

As a final note, you should know that is possible to use `T` for `TRUE` and `F` for `FALSE` but I
As a final note, you should know that is possible to use `T` for `TRUE` and `F` for `FALSE` but I
would advise against doing this, because it is not very explicit.

## Vectors and matrices
Expand Down Expand Up @@ -408,7 +426,7 @@ knitr::include_graphics("pics/rstudio_environment_list.gif")
You can also create named lists:

```{r}
list4 <- list("a" = 2, "b" = 8, "c" = "this is a named list")
list4 <- list("name_1" = 2, "name_2" = 8, "name_3" = "this is a named list")
```

and you can access the elements in two ways:
Expand All @@ -420,11 +438,11 @@ list4[[1]]
or, for named lists:

```{r}
list4$c
list4$name_3
```

The `$` operator is very useful, because it also allows you to access entire columns
of `data.frame` objects, which we are going to get to know in the next section.
Take note of the `$` operator, because it is going to be quite useful for `data.frame`s as well,
which we are going to get to know in the next section.

Lists are used extensively because they are so flexible. You can build lists of datasets and apply
functions to all the datasets at once, build lists of models, lists of plots, etc... In the later
Expand Down Expand Up @@ -484,15 +502,19 @@ or also to define anonymous functions, but more on this later.

A statistical model is an object like any other in R:

```{r}
```{r, include = FALSE}
data(mtcars)
my_model <- lm(mpg ~ hp, mtcars)
```

Here, I have already a model that I ran on some test data:

```{r}
class(my_model)
```

`my_model` is an object of class `lm`. You can apply different functions to a model object:
`my_model` is an object of class `lm`, for *linear model*. You can apply different functions to a model object:

```{r}
summary(my_model)
Expand Down Expand Up @@ -537,8 +559,8 @@ several types of `NA`s:
* `NA_complex_`
* `NA_character_`

but these are in principle only used when you need to program your own functions and need to explicitly test for the missingness of, say,
a character value.
but these are in principle only used when you need to program your own functions and need
to explicitly test for the missingness of, say, a character value.

To test whether a value is `NA`, use the `is.na()` function.

Expand Down Expand Up @@ -646,7 +668,8 @@ paste("Hello", "amigo", sep = "--")
paste0("Hello", "amigo")
```

If you provide a vector of characters, you can also use the `collapse` argument, which places whatever you provide for `collapse` between the
If you provide a vector of characters, you can also use the `collapse` argument,
which places whatever you provide for `collapse` between the
characters of the vector:

```{r}
Expand Down
2 changes: 1 addition & 1 deletion 03-reading_writing_data.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bind_data <- import_list(paths, rbind = TRUE)
str(bind_data)
```

This also add a further column called `_file` indicating the name of the file that contained the
This also adds a further column called `_file` indicating the name of the file that contained the
original data.

If something goes wrong, you might need to take a look at the underlying function `{rio}` is
Expand Down
21 changes: 16 additions & 5 deletions 04-descriptives.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -1939,7 +1939,7 @@ where `n()` is a `dplyr` function that can only be used within `summarise()`, `m

## Special packages for special kinds of data: `{forcats}`, `{lubridate}`, and `{stringr}`

### 🐈🐈🐈🐈
### `r paste0(rep(emoji::emoji("cat_face"), 4), collapse = "")`

Factor variables are very useful but not very easy to manipulate. `forcats` contains very useful
functions that make working on factor variables painless. In my opinion, the four following functions, `fct_recode()`, `fct_relevel()`, `fct_reorder()` and `fct_relabel()`, are the ones you must
Expand Down Expand Up @@ -2768,7 +2768,7 @@ contained in the cell is not the seven films, as seven separate characters, but
that happens to have seven elements. But it is still only one vector. *Zooming* in into the data
frame helps understand:

```{r}
```{r, echo = FALSE}
knitr::include_graphics("assets/zoom_list_columns.png", dpi = 80)
```

Expand Down Expand Up @@ -2828,7 +2828,7 @@ starwars <- starwars %>%
`dplyr::rowwise()` is useful when working with list-columns because whatever instructions follow
get run on the single element contained in the list. The picture below illustrates this:

```{r}
```{r, echo = FALSE}
knitr::include_graphics("assets/rowwise.png", dpi = 80)
```

Expand Down Expand Up @@ -2982,14 +2982,25 @@ are the central objects and where the analyst operates over them with functional

### Exercise 1 {-}

* Combine `mutate()` with `across()` to exponentiate every column of type `double`.
* Combine `mutate()` with `across()` to exponentiate every column of type `double` of the `gasoline` dataset.

To obtain the `gasoline` dataset, run the following lines:

```{r, eval = FALSE}
data(Gasoline, package = "plm")
gasoline <- as_tibble(Gasoline)
gasoline <- gasoline %>%
mutate(country = tolower(country))
```

```{r, eval = FALSE, include = FALSE}
gasoline %>%
mutate(across(is.double, exp))
```

* Exponeniate columns starting with the character `"l"`.
* Exponeniate columns starting with the character `"l"` of the `gasoline` dataset.

```{r, eval = FALSE, include = FALSE}
gasoline %>%
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-430-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified _bookdown_files/modern_R_files/figure-html/unnamed-chunk-456-1.png
Loading

0 comments on commit f529558

Please sign in to comment.