library(learnr) library(dplyr) library(magrittr) library(bst290) data(ess) knitr::opts_chunk$set(echo = FALSE)
Here are again some exercises that require you to fix problems with code chunks. Hint: Look for typos, missing or misplaced symbols, and check the parentheses.
In this scenario, you want to select three variables from the ess
dataset, but all you get is an error message:
ess %>% select(idno,gndr,agea
The last closing parenthesis was of course missing.
ess %>% select(idno,gndr,agea)
Now you want to link two operations, where the first selects variables and the second filters observations. But, again, something does not work:
ess %>% select(idno,cntry,agea,vote,stflife) filter(agea>=25)
The problem was that the pipe operator was missing at the end of the select()
-line, and R
did not understand that the operations should be linked together:
ess %>% select(idno,cntry,agea,vote,stflife) %>% filter(agea>=25)
Here you want to filter the data so that only women are left (where gndr
is equal to Female
), but somehow R
refuses to do what you want:
ess %>% filter(gndr="Female")
The problem was with the equal signs: If you want tell R
that something should or has to be equal to something else, you always use double equal signs (==
).
ess %>% filter(gndr=="Female")
OK, this one might be a bit mean --- but maybe you can figure it out anyways?
You want to select some variables from the ess
dataset, this time without using the pipe but by specifying the dataset explicitly in the select()
command:
select(data = ess, agea,idno,gndr)
The problem was the special way you name the dataset you want to use in select()
(or also filter()
): You have to add a dot before data
so that it reads .data = ess
, not data = ess
:
select(.data = ess, agea,idno,gndr)
A more convenient alternative is to use the pipe operator from the start:
ess %>% select(agea,idno,gndr)
That is it for this part! Next: Data visualization!
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.