Load the world_bank
dataset
library("dplyr") data(world_bank, package = "jrBig")
Convert the data frame into a dplyr
data frame
wb = tbl_df(world_bank)
world_bank
and wb
data frames to screen. What's different?
r
## wb only prints out 10 rows & and gives types
glimpse
function do? Hint: just try it on a data frame.
r
## Flips the data frame on it's side
wb
, complete the following tasks.filter
AFG
;r
filter(wb, Country.Code == "AFG")
filter(wb, Year > 1980 & Year < 1990)
select
Year.Code
column;Year
and gini
;
r
select(wb, -1)
select(wb, Year, gini)
mutate
Year2010
that is yes
for rows where Year > 2010
r
mutate(wb, Year2010 = ifelse(Year > 2010, "yes", "no"))
arrange
Year
in descending order and gini
r
arrange(wb, desc(Year), gini)
summarise
: gdp_percap
column.
r
summarise(wb, mean(gdp_percap, na.rm = TRUE))
slice
do? Tryslice(wb, 1:3)
, slice(wb, 5:10)
, slice(wb, n())
n()
do? Countssample_n
function. Can you sample
$100$ rows from the data set?gini
and gdp_percap
for each country; set na.rm=TRUE
in the mean function. Hint: group by Country.gb = group_by(wb, Year) summarise(gb, mean(gini, na.rm = TRUE), mean(gdp_percap, na.rm = TRUE))
gini
and gdp_percap
for each country per year.gb = group_by(wb, Year, Country.Code) summarise(gb, median(gini, na.rm = TRUE), median(gdp_percap, na.rm = TRUE))
r
wb %>%
group_by(Year, Country.Code) %>%
summarise(gini = median(gini, na.rm = TRUE)) %>%
summarise(max(gini, na.rm = TRUE))
and
r
wb %>%
group_by(Country.Code, Year) %>%
summarise(gini = median(gini, na.rm = TRUE)) %>%
summarise(max(gini, na.rm = TRUE))
summarise
a variable is peeled off the
group_by statement.Using the pipe operator, link the following operations together (for the wb
data set)
AFG
;Year.Code
column;Year
in descending order and gini
`r
wb %>%
select(Country == "AFG") %>%
arrange(desc(YEAR), gini)
Filter to retain data for years between 1980 and 1990;
Year
and gini
;Year2010
that is yes
for rows where Year > 2010
r
wb %>%
filter(Year > 1980 & Year < 1990) %>%
select(-1) %>%
mutate(Year2010 = ifelse(Year > 2010, "yes", "no"))
r
db = src_sqlite(path = tempfile(), create = TRUE)
wb_sqlite = copy_to(db, world_bank, temporary = FALSE)
wb_sqlite = tbl(db, "world_bank")
src_desc(db) ## Gives you some details
src_tbls(db) ## Lists the tables in the DB
r
head(wb_sqlite, 50)
collect
to get the database.Tip: Check out dplyr's CRAN page.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.