Commit 3630c0ff authored by Bernd Klaus's avatar Bernd Klaus
Browse files

some fixes for the Jan 17 course

parent e91e2a02
......@@ -98,7 +98,7 @@ rank(x)
## ----factors-------------------------------------------------------------
x <- factor(c("ab", "cd", "ab"), levels = c("ab", "cd", "ef"))
x <- factor(c("wt", "wt", "mut", "mut"), levels = c("wt", "mut"))
x
......@@ -186,6 +186,23 @@ pat$Height
pat[[2]]
pat[["Gender"]]
## ----acces_recap---------------------------------------------------------
sample_vector <- c("Alice" = 5.4, "Bob" = 3.7, "Claire" = 8.8)
sample_vector
## ----access_index, dependson="accesRecap"--------------------------------
sample_vector[1:2]
sample_vector[-(1:2)]
## ----access_boolean, dependson="acces_recap"-----------------------------
sample_vector[c(TRUE, FALSE, TRUE)]
## ----access_boolean2, dependson="acces_recap"----------------------------
sample_vector[sample_vector < 6]
## ----access_name---------------------------------------------------------
sample_vector[c("Alice", "Claire")]
## ----loadBodyfat, echo = TRUE------------------------------------------
load(url("http://www-huber.embl.de/users/klaus/BasicR/bodyfat.rda"))
bodyfat <- as_tibble(bodyfat)
......@@ -313,15 +330,22 @@ y_tibble <- as_tibble(y_mat)
names(y_tibble) <- c("Shop_1", "Shop_2", "Shop_3")
map(y_tibble, summary)
# summary by days
days <- matrix(y, nrow = 3, ncol = 5, byrow = FALSE)
days <- as_tibble(days)
names(days) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
map(days, summary)
## ----apply-test, echo = TRUE, results = 'hide', message=FALSE----------
pat <- read_csv("http://www-huber.embl.de/users/klaus/BasicR/Patients.csv")
pat
map_dbl(keep(pat, is_double), mean)
pat$Weight[2] <- mean(pat$Weight, na.rm = TRUE)
pat
map_dbl(keep(pat, is_double), mean, na.rm = TRUE)
pat <- mutate(pat, BMI = Weight / Height^2)
## ----embl_logo_ex, results='hide'----------------------------------------
......
......@@ -226,49 +226,49 @@ __Exercise: Simple R operations__
Define
* `x <- c(4, 2, 6)`
* x <- c(4, 2, 6)
and
* ` y <- c(1, 0, -1) `
* y <- c(1, 0, -1)
Decide what the result will be of the following:
* `length(x) `
* `sum(x) `
* `sum(x^2) `
* ` x+y `
* `x*y `
* ` x-2 `
* ` x^2 `
* length(x)
* sum(x)
* sum(x^2)
* x + y
* x * y
* x - 2
* x^2
Use R to check your answers.
Decide what the following sequences are and use R to check your answers:
* ` 7:11 `
* ` seq(2, 9) `
* ` seq(4, 10, by=2) `
* ` seq(3, 30, length=10) `
* ` seq(6, -4, by=-2) `
* 7:11
* seq(2, 9)
* seq(4, 10, by=2)
* seq(3, 30, length=10)
* seq(6, -4, by=-2)
Determine what the result will be of the following R expressions, and then use R to check
whether you are right:
* ` rep(2, 4) `
* ` rep(c(1, 2), 4) `
* ` rep(c(1, 2), c(4, 4)) `
* ` rep(1:4, 4) `
* ` rep(1:4, rep(3, 4)) `
* rep(2, 4)
* rep(c(1, 2), 4)
* rep(c(1, 2), c(4, 4))
* rep(1:4, 4)
* rep(1:4, rep(3, 4))
Use the `rep` function to define simply the following vectors in R.
* ` (6, 6, 6, 6, 6, 6) `
* ` (5, 8, 5, 8, 5, 8, 5, 8) `
* ` (5, 5, 5, 5, 8, 8, 8, 8) `
* (6, 6, 6, 6, 6, 6)
* (5, 8, 5, 8, 5, 8, 5, 8)
* (5, 5, 5, 5, 8, 8, 8, 8)
......@@ -346,22 +346,22 @@ rank(x)
__Exercise: Milk sales and summaries__
* Define
`x <- c(5, 9, 2, 3, 4, 6, 7, 0, 8, 12, 2, 9) `
x <- c(5, 9, 2, 3, 4, 6, 7, 0, 8, 12, 2, 9)
Decide what the result will be of the following:
* ` x[2] `
* ` x[2:4] `
* ` x[c(2, 3, 6)] `
* ` x[c(1:5, 10:12)] `
* ` x[-(10:12)] `
* x[2]
* x[2:4]
* x[c(2, 3, 6)]
* x[c(1:5, 10:12)]
* x[-(10:12)]
Use R to check your answers.
* The vector ` y <- c(33, 44, 29, 16, 25, 45, 33, 19, 54, 22, 21, 49, 11, 24, 56)` contains
* The vector y <- c(33, 44, 29, 16, 25, 45, 33, 19, 54, 22, 21, 49, 11, 24, 56) contains
sales of milk in liters for 5 days in three different shops (the first 3 values are for shops 1, 2 and 3 on
Monday, etc.). Produce a statistical summary of the sales for each day of the week and also
for each shop.
......@@ -811,7 +811,7 @@ __Exercise: Handling a small data set__
* Which variables are stored in the data frame and what are their values?
* Is there a missing weight value? If yes, replace it by the mean of the other weight values.
* Calculate the mean weight and height of all the patients.
* Calculate the `BMI = Weight / Height^2` of all the patients.
* Calculate the __BMI = Weight / Height^2__ of all the patients.
# Simple plotting in R: qplot of `r CRANpkg("ggplot2")`
......@@ -980,9 +980,9 @@ __Exercise: Simple R operations__
Use the rep function to define simply the following vectors in R.
* ` (6, 6, 6, 6, 6, 6) `
* ` (5, 8, 5, 8, 5, 8, 5, 8) `
* ` (5, 5, 5, 5, 8, 8, 8, 8) `
* (6, 6, 6, 6, 6, 6)
* (5, 8, 5, 8, 5, 8, 5, 8)
* (5, 5, 5, 5, 8, 8, 8, 8)
......@@ -1019,9 +1019,12 @@ x + cos(pi/y)
__Exercise: Milk sales__
The vector ` y<-c(33, 44, 29, 16, 25, 45, 33, 19, 54, 22, 21, 49, 11, 24, 56)` contain sales of milk
in liters for 5 days in three different shops (the first 3 values are for shops 1, 2 and 3 on
Monday, etc.). Produce a statistical summary of the sales for each day of the week and also
The vector y<-c(33, 44, 29, 16, 25, 45, 33, 19, 54, 22, 21, 49, 11, 24, 56)
contains sales of milk
in liters for 5 days in three different shops (the first 3 values are for
shops 1, 2 and 3 on
Monday, etc.). Produce a statistical summary of the sales for each day of the
week and also
for each shop.
......@@ -1061,7 +1064,7 @@ __Exercise: Handling a small data set__
* Which variables are stored in the data frame and what are their values?
* Is there a missing weight value? If yes, replace it by the mean of the other weight values.
* Calculate the mean weight and height across all the patients.
* Calculate the `BMI = Weight / Height^2` of all the patients.
* Calculate the __BMI = Weight / Height^2__ of all the patients.
__Solution: Handling a small data set__
......
This diff is collapsed.
No preview for this file type
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment