Skip to content

Commit

Permalink
added solutions to session 2
Browse files Browse the repository at this point in the history
  • Loading branch information
luisesanmartin committed Mar 21, 2023
1 parent 6540ee0 commit 0039f9d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions DataWork/Code/1-intro-to-R-solutions.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## R for Stata Users
## March 2023
## Exercise solutions
## Session: Introduction to R

## Exercise 1 ====
whr <- read.csv("/path/to/data/file")
Expand Down
53 changes: 53 additions & 0 deletions DataWork/Code/2-intro-to-R-programming-solutions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## R for Stata Users
## March 2023
## Exercise solutions
## Session: Introduction to R programming

## Exercise 1 ====
# (no coding needed for exercise)

## Exercise 2 ====
# (no coding needed for exercise)

## Exercise 3 ====
library(here)
whr <- read.csv(here("DataWork", "DataSets", "Final", "whr_panel.csv"))
# note that this will only work if exercise 2
# was executed correctly

## Exercise 4 ====
#install.packages("dplyr") # uncomment installation if needed
#install.packages("purrr") # uncomment installation if needed
library(dply)
library(purrr)

## Exercise 5 ====
# Create dataframe
df <- data.frame(replicate(50000, sample(1:100, 400, replace=TRUE)))
# Create empty vector
col_means_loop <- c()
# Loop and append means to vector (will take a few seconds)
for (column in df){
col_means_loop <- append(col_means_loop, mean(column))
}

## Exercise 6 ====
col_means_map <- map(df, mean)
# this will only work if you defined df in exercise 5

## Exercise 7 ====
zscore <- function(x) {
mean <- mean(x, na.rm = TRUE)
sd <- sd(x, na.rm = TRUE)
z <- (x - mean)/sd
return(z)
}

## Exercise 8 ====
z_scores <- whr %>%
select(health_life_expectancy, freedom) %>%
map(zscore)
whr$hle_st <- z_scores[[1]]
whr$freedom_st <- z_scores[[2]]
# this will only run if you created the function
# zscores() in exercise 7

0 comments on commit 0039f9d

Please sign in to comment.