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

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 sybtype) 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 […]

## Basic R For Stata Users: Solutions

Below are the solutions to these exercises on “Basic R For Stata Users.” #################### # # # Exercise 1 # # # #################### # Make sure packages are installed before you try to load them library(AER) library(foreign) data(“PSID1982”) write.dta(PSID1982, “c:/temp/data_stata/psid_1982.dta”) #################### # # # Exercise 2 # # # #################### summary(PSID1982) ## experience weeks occupation […]

## Basic R For Stata Users: Exercises

The speed and simplicity of Stata for the most basic modeling applications is amazing. However, for many of us who have switched to R, the flexibility, the community, and the fact that R is open source makes it, at least, a powerful complement. These exercises focus on some of the most commonly used commands in […]

## Regular Expressions Fundamentals – Solutions

Below are the solutions to these exercises on regular expressions fundamentals. #################### # # # Exercise 1 # # # #################### grep(“(?i)c”, names(islands), value = TRUE) ## [1] “Africa” “Antarctica” “Celebes” ## [4] “Celon” “Cuba” “Iceland” ## [7] “Madagascar” “Moluccas” “North America” ## [10] “Prince of Wales” “South America” “Vancouver” ## [13] “Victoria” # or […]

## Regular Expressions Fundamentals – Exercises

Regular expressions is one of the skills you need to drill and drill until they become second nature. You never know when you will need them, just that you WILL need them. In this exercise set, we will go through some of the fundamentals relying on base R only. If you are already an expert, […]