Skip to content

Commit

Permalink
added broom
Browse files Browse the repository at this point in the history
  • Loading branch information
sam81 committed Sep 19, 2020
1 parent 287db6b commit c9411ab
Show file tree
Hide file tree
Showing 11 changed files with 1,033 additions and 429 deletions.
2 changes: 1 addition & 1 deletion 00_preface.Rmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "A R Guide"
subtitle: "0.4.0"
subtitle: "0.4.1"
author: "Samuele Carcagno"
date: "`r format(Sys.time(), '%d %B, %Y')`"
site: bookdown::bookdown_site
Expand Down
36 changes: 29 additions & 7 deletions 01_getting_started_with_R.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ There are precompiled binaries for Windows, you just have to download them, then

Precompiled binaries are also available for Mac Os X, you can download them and then double click on the installer's icon to start the installation.

### Installing add-on packages
### Installing add-on packages {#installpkg}

There is a vast number of packages that implement statistical functions that are not available with the base program. They're not strictly necessary, but if you keep using R, sooner or later you will want to install some of these packages.
There is a vast number of add-on [packages](https://cran.r-project.org/web/packages/available_packages_by_name.html) (for a categorized view see [CRAN task views](https://cran.r-project.org/web/views/)) that implement statistical functions that are not available with the base program. They're not strictly necessary, but if you keep using R, sooner or later you will want to install some of these packages.

#### GNU/Linux and other Unix systems

Expand Down Expand Up @@ -64,7 +64,7 @@ This guide uses several datasets. If you want to follow the examples given in th

### Firing up and quitting R

Under GNU/Linux systems you can start R from a shell, just type `R` and press <kbd>Enter</kbd>. Under Windows you can click on the R icon to start the R GUI.
Under GNU/Linux systems you can start R from a shell, just type `R` and press <kbd>Enter</kbd>. Under Windows you can click on the R icon to start the R GUI.

You can save yourself a lot of typing by using the up arrow key &#8593; to retrieve past commands.

Expand Down Expand Up @@ -97,7 +97,10 @@ Now let's create a variable, we'll call it `foo`, and assign to it a number
foo = 5
```

the equal sign `=` is the assignment operator in R\footnote{You can also use the arrow `<-`as an assignment operator}, in the above case it means the value of `foo` is 5, `foo` is an object.
the equal sign `=` is the assignment operator in R, in the above case it means "the value of `foo` is 5", `foo` is an object. The "arrow symbol", constructed by a "less than" sign followed by a dash, `<-`, can also be used as an assignment operator, and `foo = 5` is equivalent to
```{r}
foo <- 5
```

```{block, type='rmdnote'}
In R both `=` and `<-` can be used as assignment operators. For example, the expression `x = 5` and the expression `x <- 5` are equivalent, and both are in common use, although some style guides recommend one over the other. In a few corner cases using `<-` instead of `=` may lead to different behaviors (e.g. when trying to make assignments inside function calls; something that you generally shouldn't do).
Expand Down Expand Up @@ -128,11 +131,15 @@ since the two objects we have created are both numeric we can also sum them:
foo + another_foo
```

```{block, type='rmdnote'}
In R variable names can be made up of letters, numbers, and the underscore (`_`), or the dot (`.`) characters. For example `foo`, `foo1`, `foo_1`, and `foo.1` are all syntactically valid variable names. However, there are some restrictions; for example variable names cannot start with a number, or with an underscore. Also, there are reserved keywords, such as `for`, `if`, `else`, that cannot be used as variable names. For more info see the help page for `make.names` by typing `?make.names` in the R interpreter.
```

Let's look at something more interesting, we can create an object that stores a series of numbers, for example the money we have spent each day of a week, in Euros; we can do this using the `c` function, which concatenates a series of values into a *vector*:
```{r}
expenses = c(7,8,15,20,9,45,3)
expenses = c(7, 8, 15, 20, 9, 45, 3)
```
you might want to find out how much you've spent in average during the week, this is easily accomplished with the function `mean`
you might want to find out how much you've spent on average during the week, this is easily accomplished with the function `mean`
```{r}
mean(expenses)
```
Expand Down Expand Up @@ -177,6 +184,21 @@ paste0(name, msg1)

more string facilities are presented in Section \@ref(stringprocessing).

### Using R packages

As mention in Section \@ref(installpkg) there are many add-on packages, with additional statistical and graphical functions for R. That section had some instructions on how to install them, but did not explain how to use them. In order to use a function from a package that you have already installed you can load the package with the `library` function. For example, if you want to use the `geometric.mean` function from the `psych` package in order to compute the geometric mean of a set of numbers you can run the following commands:
```{r}
library(psych)
geometric.mean(c(4, 54, 7, 15))
```

Loading a package in this way will import all the functions of that package in the current *namespace*, in other words, this lets you call directly those functions from the current session. Occasionally there may be "clashes" in the namespace; suppose there is another package called `foz` (I'm making it up) tha you also need to use in your session. Package `foz` also happens to have a function called `geometric.mean` that behaves slightly differently from the function in the `psych` package. If you load package `foz` after package `psych`, the `geometric.mean` function from `foz` will *mask* the one from `psych` (you will get a warning on the R terminal). So, what can you do if you want to use the `geometric.mean` function from `psych` when it's masked? You can call it with the following syntax:
```{r}
psych::geometric.mean(c(4, 54, 7, 15))
```

note that this syntax works also if the `psych` package is not loaded, in other words you can call functions from packages with the ``pkgname::fncname`` syntax without loading the package.

### Getting help {#gettinghelp}

R comes with an excellent online help facility which documents and gives examples for all available functions. There is also a web interface for the help system which is easier to use, you can start it with
Expand Down Expand Up @@ -239,7 +261,7 @@ levene.test(car) Levene's Test

### Working with a graphical user interface {#gui}

The default version of R for Windows and macOS comes with a very limited graphical user interface (GUI), while the GNU/Linux version comes with no GUI at all. There are however several independent projects aimed at developing a GUI for R. The following sections give some information on them.
The default version of R for Windows and macOS comes with a very limited graphical user interface (GUI), while the GNU/Linux version comes with no GUI at all. There are, however, several independent projects aimed at developing a GUI for R. The following sections give info on some of them.

#### R Studio

Expand Down
210 changes: 0 additions & 210 deletions 02_a_simple_introduction_to_R.Rmd

This file was deleted.

20 changes: 15 additions & 5 deletions 02_organizing_a_working_session.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@

The command `getwd` displays the pathname of the current working directory, that is where R will look for and store files if not otherwise instructed.

To change the current working directory, use the command `setwd("dirname")`, where `dirname` is the pathname of the working directory you're moving into. Note that this has to be an existent working directory, because R cannot create a new directory with this command. Here's an example of how to specify the pathname:
To change the current working directory, use the command `setwd("dirname")`, where `dirname` is the pathname of the working directory you're moving into. Note that this has to be an existent working directory, because R cannot create a new directory with this command. Here's an example of how to specify the pathname on Windows:
```{r eval=FALSE}
setwd("C:/mydata/rats")
```
note that you have to use a slash `"/"` and not backslash `"\"` like you usually do in Windows to specify the pathname.
You can also specify a pathname relative to your current working directory, without specifying the full pathname. It is indifferent using single `' '` or double `" "` quotes, this holds true when you need to quote character strings.
note that you have to use a slash `"/"` and not backslash `"\"` like you usually do in Windows to specify the pathname. On Unix-based OSs the pathname is specified as it would be in a shell command, e.g.:
```{r eval=FALSE}
setwd("/home/user123/Documents/analysis_exp_xyz")
```

If you want to see the files present in your current working directory, use the command `dir`.
You can also specify a pathname relative to your current working directory, without specifying the full pathname. It is indifferent using single `' '` or double `" "` quotes, this holds true when you need to quote character strings. For example to change the working directory up one level in the directory hierarchy you can write:
```{r, eval=FALSE}
setwd("../")
```

If you want to see the files present in your current working directory you can use:
```{r, eval=FALSE}
dir()
```

It is also possible to issue commands to the OS from within R with the `system` function, for example
```{r}
Expand All @@ -36,7 +46,7 @@ rm(X, Y, W, foo, rats)
```
you can also give the variables to be removed, as a character vector:
```{r eval=FALSE}
rm(list=c("X","Y","foo","rats"))
rm(list=c("X", "Y", "foo", "rats"))
```
if you want to remove all the objects in your workspace, you can combine the `ls` and `rm` commands as follows:
```{r eval=FALSE}
Expand Down
4 changes: 2 additions & 2 deletions 03_data_manipulation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ knitr::opts_chunk$set(fig.width=fig.width, fig.height=fig.height)

## Vectors

One of the simplest and among the most important data types in R is the vector, which can be numerical or containing strings of characters. A simple way to build a vector is through the `c` function, which concatenates a series of data, for example:
One of the simplest and among the most important data types in R is the vector, which can be numerical or containing strings of characters. A simple way to build a vector is through the `c` function, which concatenates a series of data values, for example:
```{r}
temperature = c(34, 45, 23, 29, 26, 31, 44, 32, 19, 22, 34)
```
Expand Down Expand Up @@ -301,7 +301,7 @@ as we said, each row holds the data of a single observation, in this case it cor

You can access, or refer to a column of a dataframe with the `$` operator, in the example above suppose we removed all the original variables after creating the dataframe
```{r}
rm(n_beers,occupation,sex)
rm(n_beers, occupation, sex)
```

we can't now access them directly by name
Expand Down
Loading

0 comments on commit c9411ab

Please sign in to comment.