-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6540ee0
commit 0039f9d
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |