c(1, 3, 5)
c(TRUE, FALSE, TRUE, TRUE)
c("red", "blue")
length(c("blue", "red"))
length(c("blue", "red"))
names(c("x" = 1, "y = 1))
length(c("blue", "red"))
names(c("x" = 1, "y = 1))
1:3
## [1] 1 2 3
1:3
## [1] 1 2 3
c(1, 2, 3)
## [1] 1 2 3
1:3
## [1] 1 2 3
c(1, 2, 3)
## [1] 1 2 3
rep(1, 3)
## [1] 1 1 1
1:3
## [1] 1 2 3
c(1, 2, 3)
## [1] 1 2 3
rep(1, 3)
## [1] 1 1 1
seq(from = 1, to = 3, by = .5)
## [1] 1.0 1.5 2.0 2.5 3.0
c()
. Use the colors "grey90" and "steelblue". Assign the vector to a name.scale_color_manual()
. Pass it using the values
argument.cols <- c("grey90", "steelblue")gapminder %>% mutate(rwanda = ifelse(country == "Rwanda", TRUE, FALSE)) %>% ggplot(aes(year, lifeExp, color = rwanda, group = country)) + geom_line() + scale_color_manual(values = cols) + theme_minimal()
[]
or [[]]
x <- c(1, 5, 7)
[]
or [[]]
x <- c(1, 5, 7)
x[2]
## [1] 5
[]
or [[]]
x <- c(1, 5, 7)
x[2]
## [1] 5
x[[2]]
## [1] 5
[]
or [[]]
x <- c(1, 5, 7)
x[2]
## [1] 5
x[[2]]
## [1] 5
x[c(FALSE, TRUE, FALSE)]
## [1] 5
x
## [1] 1 5 7
x
## [1] 1 5 7
x[2] <- 100
x
## [1] 1 5 7
x[2] <- 100
x
## [1] 1 100 7
x
## [1] 1 100 7
x
## [1] 1 100 7
x[x > 10] <- NA
x
## [1] 1 100 7
x[x > 10] <- NA
x
## [1] 1 NA 7
cols <- c("grey90", "steelblue")gapminder %>% mutate(rwanda = ifelse(country == "Rwanda", TRUE, FALSE)) %>% ggplot(aes(year, lifeExp, color = rwanda, group = country)) + geom_line() + scale_color_manual(values = cols) + theme_minimal()
cols <- c("grey90", "steelblue") gapminder %>% mutate(rwanda = ifelse(country == "Rwanda", TRUE, FALSE)) %>% ggplot(aes(year, lifeExp, group = country)) + geom_line( data = function(x) filter(x, !rwanda), color = cols[1] ) + theme_minimal()
cols <- c("grey90", "steelblue") gapminder %>% mutate(rwanda = ifelse(country == "Rwanda", TRUE, FALSE)) %>% ggplot(aes(year, lifeExp, color = rwanda, group = country)) + geom_line( data = function(x) filter(x, !rwanda), color = cols[1] ) + geom_line( data = function(x) filter(x, rwanda), color = cols[2], size = 1.5 ) + theme_minimal()
sum()
. Then add na.rm = TRUE
.is.na()
; save the results to a new object and take a lookx
to 0sum()
again without na.rm = TRUE
.x <- c(3, 5, NA, 2, NA)sum(x)
## [1] NA
sum(x, na.rm = TRUE)
## [1] 10
x_missing <- is.na(x)x_missing
## [1] FALSE FALSE TRUE FALSE TRUE
x[x_missing] <- 0x
## [1] 3 5 0 2 0
sum(x)
## [1] 10
sim_data
that doesn't take any arguments.tibble
.x
, have rnorm()
return 50 random numbers.sex
, use rep()
to create 50 values of "male" and "female". Hint: You'll have to give rep()
a character vector. for the first argument. The times
argument is how many times rep()
should repeat the first argument, so make sure you 3. account for that.age()
use the sample()
function to sample 50 numbers from 25 to 50 with replacement.sim_data()
sim_data <- function() { tibble( x = rnorm(50), sex = rep(c("male", "female"), times = 25), age = sample(25:50, size = 50, replace = TRUE) )}sim_data()
sim_data <- function() { tibble( x = rnorm(50), sex = rep(c("male", "female"), times = 25), age = sample(25:50, size = 50, replace = TRUE) )}sim_data()
sim_data <- function() { tibble( x = rnorm(50), sex = rep(c("male", "female"), times = 25), age = sample(25:50, size = 50, replace = TRUE) )}sim_data()
sim_data <- function() { tibble( x = rnorm(50), sex = rep(c("male", "female"), times = 25), age = sample(25:50, size = 50, replace = TRUE) )}sim_data()
## # A tibble: 50 x 3## x sex age## <dbl> <chr> <int>## 1 0.312 male 42## 2 -0.387 female 25## 3 -0.0210 male 38## 4 1.38 female 33## 5 0.796 male 30## 6 -0.996 female 29## 7 -0.442 male 38## 8 0.00711 female 28## 9 1.16 male 45## 10 0.116 female 49## # … with 40 more rows
evalue
and give it an argument called estimate
. In the body of the function, calculate the E-Value using estimate + sqrt(estimate * (estimate - 1))
evalue()
for a risk ratio of 3.9evalue <- function(estimate) { estimate + sqrt(estimate * (estimate - 1))}
evalue <- function(estimate) { estimate + sqrt(estimate * (estimate - 1))}
evalue(3.9)
## [1] 7.263034
if (PREDICATE) { true_result}
if (PREDICATE) { true_result}
if (PREDICATE) { true_result} else { default_result}
if (PREDICATE) { true_result}
if (PREDICATE) { true_result} else { default_result}
if (PREDICATE) { true_result} else if (ANOTHER_PREDICATE) { true_result} else { default_result}
ifelse(PREDICATE, true_result, false_result)dplyr::case_when( PREDICATE ~ true_result, PREDICATE ~ true_result, TRUE ~ default_result)switch( x, value1 = result, value2 = result)
if (is.numeric(x))
stop()
, warn()
if (is.numeric(x))
stop()
, warn()
function(x) { if (is.numeric(x)) stop("x must be a character") # do something with a character}
if ()
together with is.numeric()
to make sure estimate
is a number. Remember to use !
for not.estimate
to be equal to 1 / estimate
.evalue()
for a risk ratio of 3.9. Then try 0.80. Then try a character value.evalue <- function(estimate) { if (!is.numeric(estimate)) stop("`estimate` must be numeric") if (estimate < 1) estimate <- 1 / estimate estimate + sqrt(estimate * (estimate - 1))}
evalue(3.9)
## [1] 7.263034
evalue(.80)
## [1] 1.809017
evalue("3.9")
## Error in evalue("3.9"): `estimate` must be numeric
type
. Set the default value to "rr"type
is equal to "or". If it is, set the value of estimate
to be sqrt(estimate)
evalue()
for a risk ratio of 3.9. Then try it again with type = "or"
.evalue <- function(estimate, type = "rr") { if (!is.numeric(estimate)) stop("`estimate` must be numeric") if (type == "or") estimate <- sqrt(estimate) if (estimate < 1) estimate <- 1 / estimate estimate + sqrt(estimate * (estimate - 1))}
evalue(3.9)
## [1] 7.263034
evalue(3.9, type = "or")
## [1] 3.362342
transform_to_rr
with arguments estimate
and type
.type == "or"
and transform if so. Add another line that checks if type == "hr"
. If it does, transform the estimate using this formula: (1 - 0.5^sqrt(estimate)) / (1 - 0.5^sqrt(1 / estimate))
.estimate < 1
to transform_to_rr
(below the OR and HR transformations)estimate
evalue()
, change the default argument of type
to be a character vector containing "rr", "or", and "hr".type
using match.arg()
. Follow the pattern argument_name <- match.arg(argument_name)
estimate
using transform_to_rr()
. Don't forget to pass it both estimate
and type
!transform_to_rr <- function(estimate, type) { if (type == "or") estimate <- sqrt(estimate) if (type == "hr") { estimate <- (1 - 0.5^sqrt(estimate)) / (1 - 0.5^sqrt(1 / estimate)) } if (estimate < 1) estimate <- 1 / estimate estimate}evalue <- function(estimate, type = c("rr", "or", "hr")) { # validate arguments if (!is.numeric(estimate)) stop("`estimate` must be numeric") type <- match.arg(type) # calculate evalue estimate <- transform_to_rr(estimate, type) estimate + sqrt(estimate * (estimate - 1))}
transform_to_rr <- function(estimate, type) { if (type == "or") estimate <- sqrt(estimate) if (type == "hr") { estimate <- (1 - 0.5^sqrt(estimate)) / (1 - 0.5^sqrt(1 / estimate)) } if (estimate < 1) estimate <- 1 / estimate estimate}evalue <- function(estimate, type = c("rr", "or", "hr")) { # validate arguments if (!is.numeric(estimate)) stop("`estimate` must be numeric") type <- match.arg(type) # calculate evalue estimate <- transform_to_rr(estimate, type) estimate + sqrt(estimate * (estimate - 1))}
transform_to_rr <- function(estimate, type) { if (type == "or") estimate <- sqrt(estimate) if (type == "hr") { estimate <- (1 - 0.5^sqrt(estimate)) / (1 - 0.5^sqrt(1 / estimate)) } if (estimate < 1) estimate <- 1 / estimate estimate}evalue <- function(estimate, type = c("rr", "or", "hr")) { # validate arguments if (!is.numeric(estimate)) stop("`estimate` must be numeric") type <- match.arg(type) # calculate evalue estimate <- transform_to_rr(estimate, type) estimate + sqrt(estimate * (estimate - 1))}
evalue(3.9)
## [1] 7.263034
evalue(3.9, type = "or")
## [1] 3.362342
evalue(3.9, type = "hr")
## [1] 4.474815
evalue(3.9, type = "rd")
## Error in match.arg(type): 'arg' should be one of "rr", "or", "hr"
...
...
select_gapminder <- function(...) { gapminder %>% select(...)}select_gapminder(pop, year)
...
select_gapminder <- function(...) { gapminder %>% select(...)}select_gapminder(pop, year)
...
## # A tibble: 1,704 x 2## pop year## <int> <int>## 1 8425333 1952## 2 9240934 1957## 3 10267083 1962## 4 11537966 1967## 5 13079460 1972## 6 14880372 1977## 7 12881816 1982## 8 13867957 1987## 9 16317921 1992## 10 22227415 1997## # … with 1,694 more rows
...
to pass the arguments of your function, filter_summarize()
, to filter()
.filter_summarize()
with year == 1952
.filter_summarize()
again for 2002, but also filter countries that have "and" in the country name. Use str_detect()
from the stringr package.filter_summarize <- function(...) { gapminder %>% filter(...) %>% summarize(n = n(), mean_lifeExp = mean(lifeExp))}
filter_summarize(year == 1952)
## # A tibble: 1 x 2## n mean_lifeExp## <int> <dbl>## 1 142 49.1
filter_summarize(year == 2002, str_detect(country, " and "))
## # A tibble: 1 x 2## n mean_lifeExp## <int> <dbl>## 1 4 69.9
plot_hist <- function(x) { ggplot(gapminder, aes(x = x)) + geom_histogram()}
plot_hist <- function(x) { ggplot(gapminder, aes(x = x)) + geom_histogram()}
plot_hist(lifeExp)
## Error in FUN(X[[i]], ...): object 'lifeExp' not found
plot_hist <- function(x) { ggplot(gapminder, aes(x = x)) + geom_histogram()}
plot_hist("lifeExp")
## Error: StatBin requires a continuous x variable: the x variable is discrete.Perhaps you want stat="count"?
plot_hist <- function(x) { ggplot(gapminder, aes(x = {{x}})) + geom_histogram()}
plot_hist <- function(x) { ggplot(gapminder, aes(x = {{x}})) + geom_histogram()}
plot_hist(lifeExp)
year
using the value of .year
(notice the period before hand!). You do NOT need curly-curly for this. (Why is that?)variable
for x. For y, we'll use country
, but to keep it in the order we arranged it by, we'll turn it into a factor. Wrap the the factor()
call with fct_inorder()
. Check the help page if you want to know more about what this is doing.top_barplot <- function(variable, .year) { gapminder %>% filter(year == .year) %>% arrange(desc({{variable}})) %>% # take the 10 lowest values tail(10) %>% ggplot(aes(x = {{variable}}, y = fct_inorder(factor(country)))) + geom_point() + theme_minimal()}
top_barplot <- function(variable, .year) { gapminder %>% filter(year == .year) %>% arrange(desc({{variable}})) %>% # take the 10 lowest values tail(10) %>% ggplot(aes(x = {{variable}}, y = fct_inorder(factor(country)))) + geom_point() + theme_minimal()}
top_barplot(lifeExp, 2002)
top_barplot(lifeExp, 2002) + labs(x = "Life Expectancy", y = "Country")
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |