Course Notes
Use this workspace to take notes, store code snippets, and build your own interactive cheatsheet!
Note that the data from the course is not yet added to this workspace. You will need to navigate to the course overview page, download any data you wish to use, and add it to the file browser.
# Import any packages you want to use here
Take Notes
Add notes here about the concepts you've learned and code cells with code you want to keep.
Add your notes here
# Add your code snippets here
# The nyc list is already specified
nyc <- list(pop = 8405837,
boroughs = c("Manhattan", "Bronx", "Brooklyn", "Queens", "Staten Island"),
capital = FALSE)
# Loop version 1
for (p in nyc) {
print(p)
}
# Loop version 2
for (i in 1:length(nyc)) {
print(nyc[[i]])
I had problems with this one
# The tic-tac-toe matrix ttt has already been defined for you
# define the double for loop
for (i in 1:nrow(ttt) ) {
for (j in 1:ncol(ttt) ) {
print(paste("On row", i, "and column", j, "the board contains", ttt[i,j]))
}
}
Calculate the number of days that passed between the last and the first day you ate pizza. Print the result. Use the function diff() on pizza to calculate the differences between consecutive pizza days. Store the result in a new variable day_diff. Calculate the average period between two consecutive pizza days. Print the result.
# day1, day2, day3, day4 and day5 are already available in the workspace
# Difference between last and first pizza day
day5 - day1
# Create vector pizza
pizza <- c(day1, day2, day3, day4, day5)
# Create differences between consecutive pizza days: day_diff
day_diff <- diff(pizza)
# Average period between two consecutive pizza days
mean(day_diff)
# Summarize the median gdpPercap by year, then save it as by_year
by_year <- gapminder %>%
group_by(year) %>%
summarize(medianGdpPercap = median(gdpPercap))