Mehrere Datensätze

── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.1     ✔ stringr   1.5.2
✔ ggplot2   4.0.0     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Attaching package: 'kableExtra'


The following object is masked from 'package:dplyr':

    group_rows

Häufige Problemstellung

  • Daten auf mehrere Dateien verteilt

  • Im Beispiel hier: Niederschlagsdaten von drei Stationen

Erste Möglichkeit: Copy und Paste

d1 <- read_delim(
  "daten/produkt_nieder_tag_18850601_20171231_05344.txt", 
  delim = ";", locale = locale(decimal_mark = "."), trim_ws = TRUE
)
Rows: 44742 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ";"
chr (1): eor
dbl (6): STATIONS_ID, MESS_DATUM, QN_6, RS, RSF, SH_TAG

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
d2 <- read_delim(
  "daten/produkt_nieder_tag_19210101_20171231_01443.txt", 
  delim = ";", locale = locale(decimal_mark = "."), trim_ws = TRUE
)
Rows: 30146 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ";"
chr (1): eor
dbl (6): STATIONS_ID, MESS_DATUM, QN_6, RS, RSF, SH_TAG

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
d3 <- read_delim(
  "daten/produkt_nieder_tag_19310101_20171231_00555.txt", 
  delim = ";", locale = locale(decimal_mark = "."), trim_ws = TRUE
)
Rows: 20200 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ";"
chr (1): eor
dbl (6): STATIONS_ID, MESS_DATUM, QN_6, RS, RSF, SH_TAG

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
d <- bind_rows(d1, d2, d3)
  • Schnell gemacht

  • Änderungen im Nachhinein zeitraubend

  • Nicht empfohlen

Zweite Möglichkeit: In Funktion einlesen

read_one_file <- function(filename) {
  read_delim(filename, delim = ";", locale = locale(decimal_mark = "."), trim_ws = TRUE) |>
  mutate(
    MESS_DATUM = ymd(MESS_DATUM),
    STATIONS_ID = factor(STATIONS_ID)
  )
}
d1 <- read_one_file("daten/produkt_nieder_tag_18850601_20171231_05344.txt")
Rows: 44742 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ";"
chr (1): eor
dbl (6): STATIONS_ID, MESS_DATUM, QN_6, RS, RSF, SH_TAG

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
d2 <- read_one_file("daten/produkt_nieder_tag_19210101_20171231_01443.txt")
Rows: 30146 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ";"
chr (1): eor
dbl (6): STATIONS_ID, MESS_DATUM, QN_6, RS, RSF, SH_TAG

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
d3 <- read_one_file("daten/produkt_nieder_tag_19310101_20171231_00555.txt")
Rows: 20200 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ";"
chr (1): eor
dbl (6): STATIONS_ID, MESS_DATUM, QN_6, RS, RSF, SH_TAG

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
d <- bind_rows(d1, d2, d3)
  • Funktion: Eingabe Dateinamen, Rückgabewert Dataframe

  • Änderung nur an einer Stelle

  • Praktisch: Daten in der Funktion aufbereiten

  • Wichtig: Zuerst an einer Datei testen, es funktioniert nie auf Anhieb!

  • Empfohlen falls nur wenige Dateien

Dritte Möglichkeit: Verzeichnis auflisten lassen

read_one_file <- function(filename) {
  read_delim(filename, delim = ";", locale = locale(decimal_mark = "."), trim_ws = TRUE, show_col_types = FALSE) |>
  mutate(
    MESS_DATUM = ymd(MESS_DATUM),
    STATIONS_ID = factor(STATIONS_ID)
  )
}

d <- list.files(path = "daten", pattern = "produkt_nieder_tag.*.txt", full.names = TRUE) |>
  map(read_one_file) |>
  bind_rows()
  • Mit list.files Dateien auflisten lassen

  • Mit map die Funktion read_one_file für alle Dateien ausführen

  • Ergebnis mit bind_rows in einem Dataframe kombinieren

  • Wichtig: Für bind_rows müssen Merkmale den selben Datentyp haben. Falls notwendig mit mutate und as.character oder as.numeric konvertieren

  • Empfohlen für große Anzahl an Dateien