This is the R script repository of the "Coding 3: Introduction to R" course of the 2023/2024 Winter term, part of the MSc in Business Analytics at CEU. In the previous years, most of these materials were part of the "Data Analysis 1a: Exploration" course that you can find in the 2015/2016 Winter, 2016/2017 Fall, 2017/2018 Fall and 2018/2019 Fall branches.
2 x 300 mins on Jan 10 and 17:
In-person at the Vienna campus (QS B-421).
Please find in the syllabus
folder of this repository.
Please bring your own laptop* and make sure to install the below items before attending the first class:
#ba-r-intro-2023
)R
from https://cran.r-project.orgRStudio Desktop
(Open Source License) from https://posit.co/download/rstudio-desktop/install.packages('ggplot2')
library(ggplot2)
ggplot(diamonds) +
aes(x = price, fill = clarity) +
geom_density(alpha = 0.5) + facet_grid(color ~ cut) +
xlab('') + ylab('') +
theme_bw() + theme('legend.position' = 'top') +
guides(fill = guide_legend(nrow = 1))
Optional steps I highly suggest to do as well before attending the class if you plan to use git
:
git
from https://git-scm.com/git
executable binary in the Tools/Global Options menu's "Git/Svn" tab -- if not, then you might have to restart RStudio (if you installed git after starting RStudio) or installed git by not adding that to the PATH on Windows. Either way, browse the "git executable" manually (in some bin
folder look for thee git
executable file).If RStudio/git is complaining that you have to set your identity, click on the "Git" tab in the top-right panel, then click on the Gear icon and then "Shell" -- here you can set your username and e-mail address in the command line, so that RStudio/git integration can work. Use the following commands:
$ git config --global user.name "Your Name"
$ git config --global user.email "Your e-mail address"
Close this window, commit, push changes, all set.
Find more resources in Jenny Bryan's "Happy Git and GitHub for the useR" tutorial if in doubt or contact me.
(*) If you may not be able to use your own laptop, there's a shared RStudio Server set up in AWS - including all the required R packages already installed for you. Look up the class Slack channel for how to access.
For the curious mind, this is how the shared RStudio Server was set up in AWS: Click to expand ...
💪 Installing software:
# most recent R builds
wget -q -O- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
echo "deb [arch=amd64] https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/" | sudo tee -a /etc/apt/sources.list.d/cran_r.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 67C2D66C4B1D4339 51716619E084DAB9
sudo apt update && sudo apt upgrade
sudo apt install r-base
# apt builds of all CRAN packages
wget -q -O- https://eddelbuettel.github.io/r2u/assets/dirk_eddelbuettel_key.asc | sudo tee -a /etc/apt/trusted.gpg.d/cranapt_key.asc
echo "deb [arch=amd64] https://r2u.stat.illinois.edu/ubuntu jammy main" | sudo tee -a /etc/apt/sources.list.d/cranapt.list
sudo apt update
# install some R packages
sudo apt install -y r-cran-ggplot2 r-cran-ggally r-cran-readxl
sudo apt install -y r-cran-data.table
sudo apt install -y r-cran-glue r-cran-logger
# install RStudio IDE
sudo apt install -y r-base gdebi-core
wget https://download2.rstudio.org/server/jammy/amd64/rstudio-server-2023.12.0-369-amd64.deb
sudo gdebi rstudio-server-*.deb
# never do this in prod
echo "www-port=80" | sudo tee -a /etc/rstudio/rserver.conf
sudo rstudio-server restart
💪 Creating users
secret <- 'something super secret' # e.g. digest::digest(1, algo="sha1")
users <- c('list', 'of', 'users')
library(logger)
library(glue)
for (user in users) {
## remove invalid character
user <- sub('@.*', '', user)
user <- sub('-', '_', user)
user <- sub('.', '_', user, fixed = TRUE)
user <- tolower(user)
log_info('Creating {user}')
system(glue("sudo adduser --disabled-password --quiet --gecos '' {user}"))
log_info('Setting password for {user}')
system(glue("echo '{user}:{secret}' | sudo chpasswd")) # note the single quotes + placement of sudo
log_info('Adding {user} to sudo group')
system(glue('sudo adduser {user} sudo'))
}
ggplot2
: 1.Rdata.table
: 1.RSuggested reading: Hadley Wickham: Style guide. In Advanced R.
data.table
recap: 2.Rdata.table
multiple summaries (optional): 2.RLoad the bookings dataset:
library(ggplot2)
library(data.table)
hotels <- readRDS(url('http://bit.ly/CEU-R-hotels-2018-merged'))
weighted.mean
function to account for the number of ratings of the hotels, and experiment with the na.rm
argument. Eliminate NA
s. Order by stars.Compare your results with the example solutions.
Use any publicly accessible dataset (preferably from the TidyTuesday projects at https://github.com/rfordatascience/tidytuesday, but if you don't feel creative, feel free to default to using the diamonds
from the ggplot2
package) and do data transformations that seems useful, optionally merge external datasets, generate data visualizations that makes sense and are insightful, plus provide comments on those in plain English.
Submission: prepare an R markdown document that includes plain English text description of the dataset, problems/questions you analyzed, actual R code chunks (printing both the code and its output) loading the data, doing the analysis, comments and summary/conclusion of the results, and knit the Rmd to HTML, then upload both the Rmd and the HTML to Moodle before Feb 4, 2024 midnight (CET). Please don't leave the submission for the last minute, and be sure to submit by Jan 26, 2024 (Friday) if you would like to get some feedback before the final deadline.
Required items:
data.table
,data.table
,ggplot2
geoms (e.g. a scatterplot, boxplot, barchart etc.)The above items with proper homework solutions from the first week will result in "B" grade.
For "A", please also work on the below extra items:
File a GitHub ticket.