Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly expands the first practical #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion 1-3_Working_with_R_practical.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ https://stat.ethz.ch/R-manual/R-devel/library/survival/html/heart.html

https://stat.ethz.ch/R-manual/R-devel/library/survival/html/retinopathy.html



## Basic calculations

### Expressions {.tabset .tabset-fade .tabset-pills}
Expand Down Expand Up @@ -106,13 +108,76 @@ d <- (a - b) * c
`a`, `b`, `c`, and `d` are variables. To see their values, you can just type the variable name (e.g. `a`) and hit {{< kbd Ctrl-Enter >}} or use the command `print(a)`.


:::

::: panel-tabset

R can do calculations on whole vectors at once.

#### Task 3

Combine the two values 3 and 4 into a vector `v1` using the `c` function. Assign the name `v1` to the vector. Then calculate the square of each value in the vector.

#### Hint 3

Execute the following code.

```{r}
v1 <- c(3, 4)
# now compute the square of each value
```

:::


## Loading packages and using the help function

### Loading packages {.tabset .tabset-fade .tabset-pills}

We will use data sets from the `survival` package. It is important to know how to load packages as they contain most of the functionality of R.


::: {.panel-tabset .nav-pills}
#### Task 1

Load the `survival` package using the `library` function.

#### *Hint 1*

Use the `library(...)` function. There should be no need to install the package as it is already installed with `R`.

:::

### Getting help {.tabset .tabset-fade .tabset-pills}

::: {.panel-tabset .nav-pills}
#### Task 2

Ask for the documentation of the `heart` data set, using the `help` function. Then do the same for the `retinopathy` data set using `?`.

#### *Hint 2*

Use the `help(...)` function and `?`. Remember that the first form requires quotation marks.

:::

::: {.panel-tabset .nav-pills}
#### Task 3

Also ask for help on the `c`, `ls` and `rm` functions. Check if you can also obtain the documentation for the `c` function if you use `??combine`

#### *Hint 3*

Use the `help(...)` fuction, and `?` or `??`.

:::

## Importing and Saving Data


### Save your work {.tabset .tabset-fade .tabset-pills}

It is important to save your work.
It is important to save your work. You can save the whole workspace using the function `save.image`. If you want to save only specific objects, you can use the function `save`.

::: {.panel-tabset .nav-pills}
#### Task 1
Expand Down Expand Up @@ -148,6 +213,9 @@ Load the file `new_vectors`.
Use the function `load(...)`.
:::

At this point it is interesting to check the objects that are in the workspace. To do so look at the Environment tab in the upper-right hand pane. You can also use the `ls()` function to list all objects in the workspace.


### Remove your work {.tabset .tabset-fade .tabset-pills}

Remove unnecessary objects.
Expand All @@ -171,3 +239,6 @@ Remove the vectors `events` and `eyes`.

Use the function `rm(...)`.
:::


At this point you can inspect the History tab in the upper-right hand pane to see the commands you have executed. If you have made a syntax file check if you can run it in it's entirety without any errors (if you have not you can piece it together from the history). Save the file to disk.
106 changes: 102 additions & 4 deletions 1-4_Working_with_R_answers.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ d <- (a - b) * c

`a`, `b`, `c`, and `d` are variables. To see their values, you can just type the variable name (e.g. `a`) and hit {{< kbd Ctrl-Enter >}} or use the command `print(a)`.

#### Solution 2 {.unnumbered}
#### Solution 2 {r abc, solution = TRUE, eval=FALSE, .unnumbered}

```{r}
a <- 45
Expand All @@ -129,11 +129,104 @@ d
:::


::: panel-tabset

R can do calculations on whole vectors at once.

#### Task 3

Combine the two values 3 and 4 into a vector `v1` using the `c` function. Assign the name `v1` to the vector. Then calculate the square of each value in the vector.

#### Hint 3

Execute the following code.

```{r}
v1 <- c(3, 4)
# now compute the square of each value
```

#### Solution 3 {r vectorscalc, solution = TRUE, eval=FALSE, .unnumbered}

```{r}
v1 <- c(3, 4)
v1^2
```

:::

## Loading packages and using the help function

### Loading packages {.tabset .tabset-fade .tabset-pills}

We will use data sets from the `survival` package. It is important to know how to load packages as they contain most of the functionality of R.


::: {.panel-tabset .nav-pills}
#### Task 1

Load the `survival` package using the `library` function.

#### *Hint 1*

Use the `library(...)` function. There should be no need to install the package as it is already installed with `R`.

#### Solution 1 {.unnumbered}

```{r, solution = TRUE, eval=FALSE}
library(survival)
# library("survival") is an alternative in this case
# require(survival) is another alternative which does not give an error if the package cannot be found
```

:::

### Getting help {.tabset .tabset-fade .tabset-pills}

::: {.panel-tabset .nav-pills}
#### Task 2

Ask for the documentation of the `heart` data set, using the `help` function. Then do the same for the `retinopathy` data set using `?`.

#### *Hint 2*

Use the `help(...)` function and `?`.

#### Solution 2 {.unnumbered}

```{r help, solution = TRUE, eval=FALSE}
help("heart")
?retinopathy
```

:::

::: {.panel-tabset .nav-pills}
#### Task 3

Also ask for help on the `c`, `ls` and `rm` functions. Check if you can also obtain the documentation for the `c` function if you use `??combine`

#### *Hint 3*

Use the `help(...)` fuction, and `?` or `??`.

#### Solution 3 {.unnumbered}

```{r help2, solution = TRUE, eval=FALSE}
?c # or use help("...")
?ls
?rm
??combine
# or use help.search('...')
```

:::

## Importing and Saving Data

### Save your work {.tabset .tabset-fade .tabset-pills}

It is important to save your work.
It is important to save your work. You can save the whole workspace using the function `save.image`. If you want to save only specific objects, you can use the function `save`.

::: {.panel-tabset .nav-pills}
#### Task 1
Expand All @@ -146,7 +239,7 @@ Use the function `save(...)`. Note that you need to set the working directory.

#### Solution 1

```{r save-solution, solution = TRUE, eval=F}
```{r save-solution, solution = TRUE, eval=FALSE}
numbers <- c(34, 24, 19, 23, 16)
numbers_2 <- c(1:200)
treatment <- c("yes", "yes", "no", "no", "no", "yes")
Expand All @@ -165,7 +258,7 @@ Use the function `save(...)`. Note that you need to set the working directory.

#### Solution 2 \*

```{r save2-solution, solution = TRUE, eval=F}
```{r save2-solution, solution = TRUE, eval=FALSE}
events <- heart$event
eyes <- retinopathy$eye
save(events, eyes, file = "vectors_survival.RData")
Expand All @@ -192,6 +285,8 @@ load("new_vectors.RData")
```
:::

At this point it is interesting to check the objects that are in the workspace. To do so look at the Environment tab in the upper-right hand pane. You can also use the `ls()` function to list all objects in the workspace.

### Remove your work {.tabset .tabset-fade .tabset-pills}

Remove unnecessary objects.
Expand Down Expand Up @@ -227,3 +322,6 @@ Use the function `rm(...)`.
rm(events, eyes)
```
:::


At this point you can inspect the History tab in the upper-right hand pane to see the commands you have executed. If you have made a syntax file check if you can run it in it's entirety without any errors (if you have not you can piece it together from the history). Save the file to disk.