── 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
Mehrere Datensätze
Häufige Problemstellung
Daten auf mehrere Dateien verteilt
Im Beispiel hier: Niederschlagsdaten von drei Stationen
Erste Möglichkeit: Copy und Paste
<- read_delim(
d1 "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.
<- read_delim(
d2 "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.
<- read_delim(
d3 "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.
<- bind_rows(d1, d2, d3) d
Schnell gemacht
Änderungen im Nachhinein zeitraubend
Nicht empfohlen
Zweite Möglichkeit: In Funktion einlesen
<- function(filename) {
read_one_file read_delim(filename, delim = ";", locale = locale(decimal_mark = "."), trim_ws = TRUE) |>
mutate(
MESS_DATUM = ymd(MESS_DATUM),
STATIONS_ID = factor(STATIONS_ID)
)
}<- read_one_file("daten/produkt_nieder_tag_18850601_20171231_05344.txt") d1
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.
<- read_one_file("daten/produkt_nieder_tag_19210101_20171231_01443.txt") d2
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.
<- read_one_file("daten/produkt_nieder_tag_19310101_20171231_00555.txt") d3
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.
<- bind_rows(d1, d2, d3) d
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
<- function(filename) {
read_one_file 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)
)
}
<- list.files(path = "daten", pattern = "produkt_nieder_tag.*.txt", full.names = TRUE) |>
d map(read_one_file) |>
bind_rows()
Mit
list.files
Dateien auflisten lassenMit
map
die Funktionread_one_file
für alle Dateien ausführenErgebnis mit
bind_rows
in einem Dataframe kombinierenWichtig: Für
bind_rows
müssen Merkmale den selben Datentyp haben. Falls notwendig mitmutate
undas.character
oderas.numeric
konvertierenEmpfohlen für große Anzahl an Dateien