Below are the solutions to these exercises on “Create and Format a Google Sheet Within R.” #################### # # # Exercise 1 # # # #################### library(googlesheets) suppressMessages(library(dplyr)) #################### # # # Exercise 2 # # # #################### gs_auth(new_user = TRUE) #################### # # # Exercise 3 # # # #################### # Assignment is important […]

## Create and Format a Google Sheet Within R: Exercises

In this exercise set, we will practice using the Google Sheets package to create and manipulate a Google spreadsheet within R. After completing this exercise set, you will be able to prepare a basic Google Sheets document using just R, leaving behind a reproducible R-script. Note that using Google Sheets is free of cost, but […]

## Well-Behaved Functions – Solutions

Below are the solutions to these exercises on “Well Behaved Functions.” #################### # # # Exercise 1 # # # #################### powerof <- function(a, b = 2) { a^b } powerof(4) ## [1] 16 powerof(2, 3) ## [1] 8 #################### # # # Exercise 2 # # # #################### powerof <- function(a = b + […]

## Well-Behaved Functions – Exercises

It is said that, in R, everything that happens is a function call. So, if we want to improve our ability to make things happen the way we want them to, maybe it’s worth getting very comfortable with how functions work in R. In this exercise set, we’ll try to gain better fluency and deepen […]

## K-Means Clustering in R: Solutions

Below are the solutions to these exercises on “K-Means Clustering in R.” #################### # # # Exercise 1 # # # #################### set.seed(1) km1 <- kmeans(iris[, grep(“Sepal”, names(iris))], centers = 3) clusters1 <- km1[[“cluster”]] #################### # # # Exercise 2 # # # #################### iris_ds2 <- data.frame(iris, Cluster = factor(clusters1)) # Using table from base […]

## K-Means Clustering in R – Exercises

K-means is efficient, and perhaps, the most popular clustering method. It is a way for finding natural groups in otherwise unlabeled data. You specify the number of clusters you want defined and the algorithm minimizes the total within-cluster variance. In this exercise, we will play around with the base R inbuilt k-means function on some labeled […]

## Loops in R – Solutions

[emaillocker]Below are the solutions to these exercises on loops. #################### # # # Exercise 1 # # # #################### for (i in 1:7) { print(i^3) } ## [1] 1 ## [1] 8 ## [1] 27 ## [1] 64 ## [1] 125 ## [1] 216 ## [1] 343 #################### # # # Exercise 2 # # […]

## Loops in R – Exercises

Using loops is generally discouraged in R when it is possible to avoid them using vectorized alternatives. Vectorized solution are be both faster to write, read and execute – except sometimes they aren’t and the definition of vectorization isn’t always straightforward. In any event, solutions using loops can be: The fastest to prototype The easiest […]

## Know your lists – Solutions

Below are the solutions to these exercises on “Know your lists”. #################### # # # Exercise 1 # # # #################### x <- list(a = 1, b = 2) x ## $a ## [1] 1 ## ## $b ## [1] 2 #################### # # # Exercise 2 # # # #################### x[“c”] <- 3 x ## […]

## Know your lists – Exercises

Lists (aka recursive vectors) are the main data structure in R. Since lists are omnipresent (data.frames are a special sub-type) having a deeper understanding of them will make for a more enjoyable data analysis and helps avoid bugs. Solutions are available here. Exercise 1 Create a list called x with two elements; two vectors of […]