Skip to content

Instantly share code, notes, and snippets.

@graebnerc
Created July 8, 2024 19:25
Show Gist options
  • Select an option

  • Save graebnerc/82fb7417e43d6cd0a0e08973751360cf to your computer and use it in GitHub Desktop.

Select an option

Save graebnerc/82fb7417e43d6cd0a0e08973751360cf to your computer and use it in GitHub Desktop.
Einführung in R (Frühjahrssemester 2024): Tag 2
Hier sind alle Notizen und Aufgabenlösungen zu Tag 2 (8. Juli 2024).
# Notes taken during the recap session on what we did last week
# 1. Attaching packages---------------
# This works since `data.frame` is from base R
x1 <- data.frame("a"=1:3)
x1
# This does not work since as_tibble is from package tibble
x1_t <- as_tibble(x1)
# This works, assuming that the package tibble was installed previously (with install.packages("tibble"))
x1_t <- tibble::as_tibble(x1)
x1_t
# This then does not work
x2_t <- as_tibble(x1)
# If you attach tibble with library() you can always refer to its functions by name:
library(tibble)
x2_t <- as_tibble(x1)
x2_t
# 2. Masking problems---------------
library(dplyr) # Masks filter from package stats
filter # Refers to function from package dplyr
stats::filter # Refers to function from package stats
# Restart your R session now. Then execute the following two lines
library(dplyr)
library(ggpubr)
mutate # refers to mutate from package ggpubr
# Restart your R session now. Then execute the following two lines
library(ggpubr)
library(dplyr)
mutate # refers to mutate from package dplyr
# The later package attachment masks all names from previous attachment
# To be on the save side always use:
dplyr::mutate()
ggpubr::mutate()
here::i_am("R/Day2_2-Notes-Solutions-DataImport.R")
library(here)
library(data.table)
library(tibble)
# Exercise 1-------
data_path <- here("data/raw/fread_expls-1.csv")
ex_1_dt <- fread(file = data_path)
ex_1_dt <- as_tibble(fread(file = data_path))
head(ex_1_dt)
# Exercise 2-------
data_set_2 <- tibble::as_tibble(
data.table::fread(
file = here::here("data/raw/fread_expls-2.csv")
)
)
head(data_set_2) # Gives not the correct result
data_set_2 <- tibble::as_tibble(
data.table::fread(
file = here::here("data/raw/fread_expls-2.csv"),
sep = "'"
)
)
data_set_2 # Better, but still not fully correct
# Final:
data_set_2 <- tibble::as_tibble(
data.table::fread(
file = here::here("data/raw/fread_expls-2.csv"),
sep = "'",
dec = ":"
)
)
data_set_2 # The correct solution
# If you want to get rid of the :: notation, make sure you use library() in the
# beginning:
file_name_2 <- here("data/raw/fread_expls-2.csv")
data_set_2 <- as_tibble(
fread(
file = file_name_2,
sep = "'",
dec = ":"
)
)
data_set_2
# In case you want to encode the column 'exports' as character you can use
# colClasses like this (but be aware that dec then has no effect because
# it only refers to numeric columns):
# Preview colClasses:
file_name_2 <- here("data/raw/fread_expls-2.csv")
data_set_2 <- as_tibble(
fread(
file = file_name_2,
sep = "'",
dec = ":",
colClasses = c("exports"="character")
)
)
data_set_2
# Exercise 3-------
data_file_3 <- here("data/raw/fread_expls-3.csv")
data_set_3 <- as_tibble(data.table::fread(
file = data_file_3,
colClasses = c(
# "cgroup"="factor", # Better keep as character; use factor only when strictly necessary
"commoditycode"="character")
)
)
head(data_set_3)
data_set_3 <- tibble::as_tibble(data_set_3) # I would suggest to do this in the beginning
head(data_set_3)
# Additional fourth example-------
# Used to illustrate skip
# Inspect the csv file, identify the header to be skipped (here: 2 lines):
as_tibble(fread(file = "data/raw/fread_expls-4.csv", skip = 2))
# Further remarks-------
# To only read the first two lines (useful to experiment with large data sets):
as_tibble(fread(file = "data/raw/fread_expls-1.csv", nrows = 2))
# To only read the columns c_code and exports:
as_tibble(fread(file = "data/raw/fread_expls-1.csv", select = c("c_code", "exports")))
here::i_am("R/Day2_3-Exercises-Notes-Reshaping.R")
library(here)
library(tidyr)
library(dplyr)
library(tibble) # Not strictly necessary because this package is automatically attached by tidyr/dplyr
# Exercise I-----------------
# Step 1: Read data
data_raw <- data.table::fread(file = here::here("data/raw/data_raw_long.csv"))
data_raw <- tibble::as_tibble(data_raw)
data_raw
# Step 2: make wider
data_v1 <- tidyr::pivot_wider(
data = data_raw,
names_from = "country",
values_from = "value")
head(data_v1)
# Step 3: filter out
data_v2 <- dplyr::filter(
data_v1,
variable != "gini")
data_v2
unique(data_v2$variable) # To check whether Gini has been filtered out successfully
# Variant to make the data tidy
data_tidy <- tidyr::pivot_wider(
data = data_raw,
names_from = "variable",
values_from = "value")
data_tidy
# Exercise II----------------
data_raw_2 <- data.table::fread(
file = here("data/raw/data_raw_wide.csv"),
header = TRUE)
data_raw_2 <- tibble::as_tibble(data_raw_2)
data_raw_2
data_tidy_2 <- tidyr::pivot_longer(
data = data_raw_2,
cols = c("2017", "2018")) # Better: cols = as.character(2017:2018)
data_tidy_2
# Best option:
data_tidy_2 <- tidyr::pivot_longer(
data = data_raw_2,
cols = -"country", # All columns except "country"
names_to = "year", # Just to rename the new column
values_to = "Gini"# Just to rename the new column
)
data_tidy_2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment