Skip to content

Instantly share code, notes, and snippets.

@graebnerc
Created July 1, 2024 14:30
Show Gist options
  • Save graebnerc/2c8463e8246364013cbd3527966a9ee6 to your computer and use it in GitHub Desktop.
Save graebnerc/2c8463e8246364013cbd3527966a9ee6 to your computer and use it in GitHub Desktop.
Einführung in R (Frühjahrssemester 2024): Tag 1
Hier sind alle Notizen und Aufgabenlösungen zu Tag 1 (1. Juli 2024).
## Exercises I - Solutions-----------
ex_1_1 <- 4 + 8
ex_1_1
ex_1_2 <- (-20)*3 # Klammer hier nicht notwendig
ex_1_2
ex_1_2 <- (5*3)*(5*3)
ex_1_2 <- (5*3)**2
ex_1_2 <- (5*3)^2 # Nicht empfehlenswert
ex_1_2
ex_1_3 <- (8**2 + 5**4)/3
ex_1_3
# 4. Punkt:
ex_1_4 <- sum(1:10)
ex_1_3
# Block 1.2: Grundlagen der Programmiersprache R-----------
## Mathematischen Operationen-----------
25 + 9 # Addition
99 - 9 # Substraktion
25*5 # Multiplikation
66/9 # Division
5**2 # Potenzen
(2+5)*2 # Klammern
# Hinweise zu Klammern: es ist empfehlenswert eher zu viele Klammern
# zu setzen als zu wenige, da nicht unmittelbar ersichtlich ist
# wie die folgenden Ausdrücke unterschiedliche Ergebnisse liefern:
-20**2
(-20)**2
# Zuweisungen/Assignments-----------
zwischenergebnis <- 2 + 2
zwischenergebnis <- "Hallo!" # Überschreibt Zuweisung von vorheriger Zeile
# Die einem Namen zugewiesenen Objekte können "weiterverwendet" werden:
zwischenergebnis <- 2 + 2
2*zwischenergebnis
# 3 Möglichkeiten der Zuweisung:
zwischenergebnis <- 5**2
zwischenergebnis = 5**2 # Nicht empfohlen!
5**2 -> zwischenergebnis # Nicht empfohlen!
# Namenskonventionen:
# https://style.tidyverse.org/syntax.html#object-names
# Block 1.3: Projektmanagement für R Projekte
# Die erste Zeile eines jeden Skripts sollte sein:
here::i_am("R/Day1_Block1-3_Notes.R")
# vorausgesetzt das Skript heißt 'Day1_Block1-3_Notes.R' und liegt
# im Unterordner "R"
# Danach sollten relative Pfade als Argument der Funktion
# here::here() übergeben werden, die dann automatisch
# absolute Pfade kreiert, die zum ausführenden PC passen:
library(here)
data_path <- here("data/tidy/growth_data_1.csv")
data_path # Passt zum ausführenden PC
# Mehr Informationen zum Paket:
# https://here.r-lib.org/
# Exercises III - Solutions-----------
# Create a vector containing the numbers 2, 5, 2.8 and 11.9.
ex_3_1 <- c(2, 5, 2.8, 11)
# What is the type of this vector?
typeof(ex_3_1)
# Transform this vector into the type integer. What happens?
as.integer(ex_3_1)
# Note that only the decimal part gets removed, no rounding takes place!!
# Do you think you can create a vector containing the following elements:
# "2", "Hallo", 4.0, and TRUE? Why? Why not?
ex_3_3 <- c("2", "Hallo", 4.0, TRUE)
ex_3_3
typeof(ex_3_3)
# Given the hierarchy of data types, the atomic vector takes the type of the
# element highest on the hierarchy (see the tutorial for more details)
# Exercises IV - Solutions-----------
# Create a data frame with two columns, one called "nb" containing the numbers
# 1 to 5 as double, the other called "char" containing the numbers
# 6 to 10 as character
ex4_df <- data.frame(
"nb" = 1:5,
"char" = as.character(6:10)
)
ex4_df
# Alternative:
ex4_list <- list(
"nb" = 1:5,
"char" = as.character(6:10)
)
ex4_df2 <- as.data.frame(ex4_list)
# Transform this data frame into a tibble!
library(tibble)
ex4_tib <- tibble::as_tibble(ex4_df)
ex4_tib
# Extract the second column of this tibble such that you have a vector
ex4_tib[["char"]]
# Block 1.4: Grundlegende Datentypen--------
## Funktionen--------
t_vec <- c(5, 4, 3, 4)
t_vec_na <- c(5, 4, 3, 4, NA)
# Infos zur Funktion und ihren Argumenten:
?mean
# Notwendige Argumente können, aber müssen nicht mit Namen
# verwendet werden:
mean(t_vec)
mean(x = t_vec) # Äquivalent
# Optionale Argumente bestimmen die Funktionsroutine:
sum(t_vec_na, na.rm = FALSE)
sum(t_vec_na, na.rm = TRUE)
sum(t_vec_na) # Zeigt, dass die Default-Option für na.rm FALSE ist
## Vektoren------------------
### Atomic vectors-----------
# Definitionsgemäß enhält ein atomic vector nur einen von vier Grundtypen:
# Auf Typ testen: is.*()
# In anderen Typ konvertieren: as.*()
#### Logische Werte: logical-----------
logical_vec <- c(TRUE, FALSE)
4>2
x <- 3
x == 5
TRUE + TRUE + FALSE # Bei Addition zählt TRUE als 1, FALSE als 0
### Ganze Zahlen: integer------------
integer_vec <- c(5L, 2L)
integer_vec
no_integer_vec <- c(5, 2)
is.integer(no_integer_vec)
typeof(no_integer_vec)
#### Dezimalzahlen: double-----------
double_vec <- c(2.6, 2.9)
double_vec
typeof(double_vec)
is.double(double_vec)
is.logical(double_vec)
#### Wörter: character-----------
character_vec <- c("2", "Hallo!")
character_vec
typeof(character_vec)
# Atomische Vektoren sind immer nur ein Typ:
test_vec <-c("A", 2, 5.3)
test_vec
typeof(test_vec)
### Generic vectors/lists
# Lists can contain more than one type:
list_vec <- list("A", 2, 5.3)
typeof(list_vec)
# Häufig sind Elemente benannt:
list_vec_2 <- list(
"element_1" = c("A", "B"),
"element_2" = 2,
"element_3" = c(TRUE, FALSE))
list_vec_2[["element_2"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment