First, install the following two packages with the command install.packages("tidyverse") and install.packages("gapminder") in the console below.1 Alternatively, you will probably already get a yellow banner at the top of this file indicating you need to install the packages, and can install them by clicking Install. Don’t install any package in an R chunk in this document, since it needs to be installed into R Studio.
Then, load the package by running (clicking the green play button) the chunk below:
library("gapminder") # for datasetgapminder <- gapminder # explicitly save data as a dataframe
Warm Up to dplyr with gapminder Again
Question 1
Let’s look at the data again by running the following chunk. glimpse() is a suped-up tidyverse version of str(). You can also start to see how to use the pipe operator %>%.
Now select() only the variables year, lifeExp, and country.
gapminder %>%select(year, lifeExp, country)
Question 3
Now select() all variables exceptpop.
gapminder %>%select(-pop)
Question 4
rename() the variable continent to cont.
gapminder %>%rename(cont = continent)
Question 5
arrange() the data by year.
gapminder %>%arrange(year)
Question 6
Now arrange() by year, but in descending order.
gapminder %>%arrange(desc(year))
Question 7
Now arrange() by year, then by lifeExp
gapminder %>%arrange(year, lifeExp)
Question 8
Let’s try subsetting some rows. filter() observations with pop greater than 1 billion (9 zeros).
gapminder %>%filter(pop >1000000000)
Question 9
Redo the same command from question 8, but of that subset of data, only look at India.
gapminder %>%filter(pop >1000000000, country =="India")
Question 10
Let’s pipe a bunch of commands together. select() your data to look only at year, gdpPercap, and country in the year 1997, for countries that have a gdpPercap greater than 20,000, and arrange() them alphabetically.