# prepare some helper constants:
rows_n <- 4
group_n <- 2
constant_factor <- 104 tidy1
tidy
datawrangling
schoice
Schlüsselwörter
Aufgaben, Statistik, Prognose, Modellierung, R, Datenanalyse, Regression
4.1 Aufgabe
Das Konzept von “tidy” Daten (“Tidyformat”) spielt in der Datenanalyse eine wichtige Rolle.
Betrachten Sie die Tabellen im Folgenden. Welche ist “tidy”?
Hinweise:
- Alle Variablen sollen nicht konstant sein, also mehr als einen uniquen Wert aufweisen.
- Alle Variablen sollen keine fehlenden Werte aufweisen, also komplett sein.
- Alle Variablen sollen numerisch sein.
untidy_df1 <-
tidy_df %>%
pivot_wider(names_from = group,
values_from = y)
untidy_tibbles <-
list() %>%
append(list(untidy_df1))
tibbles_listdf <-
tibbles_listdf %>%
bind_rows(tibble(type = "untidy", data = list(untidy_df1),
explanation = "Ein Tibble im breiten Format. Die Spalten `1` und `2` sind eigentlich eine Variable."))empty_row_df <-
tidy_df %>%
# mutate(across(everything(),
# as.numeric)) %>%
filter(row_number() == 1) %>%
mutate(across(everything(),
~ assign_in(.x, where = 1, value = NA)))col_chosen <- sample(cols_n, 1)
empty_col_df <-
tidy_df %>%
select(all_of(col_chosen))
empty_col_df[1] <- NAempty_col_noname_df <-
tidy_df %>%
mutate(` ` = NA ) %>%
select(last_col())untidy_df2 <-
tidy_df %>%
bind_rows(empty_row_df) %>%
sample_n(size = nrow(.))
untidy_tibbles <-
untidy_tibbles %>%
append(list(untidy_df2))
tibbles_listdf <-
tibbles_listdf %>%
bind_rows(tibble(type = "untidy", data = list(untidy_df2),
explanation = "In einem Tidy-Tibble darf keine leere Zeile vorkommen."))untidy_df3 <-
tidy_df %>%
bind_cols(empty_col_noname_df) %>%
relocate(last_col(), .after = col_random)
untidy_tibbles <-
untidy_tibbles %>%
append(list(untidy_df3))
tibbles_listdf <-
tibbles_listdf %>%
bind_rows(tibble(type = "untidy", data = list(untidy_df3),
explanation = "In einem Tidy-Tibble darf keine leere Spalte vorkommen."))untidy_df9 <-
tidy_df %>%
mutate(across(!!col_names_random,
.fns = ~max(.)))
untidy_tibbles <-
untidy_tibbles %>%
append(list(untidy_df9))
tibbles_listdf <-
tibbles_listdf %>%
bind_rows(tibble(type = "untidy", data = list(untidy_df9),
explanation = "Eine Spalte soll nicht aus einem uniquen Wert bestehen."))untidy_df10 <-
tidy_df %>%
mutate(!!col_names_random := as.character(!!col_names_random))
untidy_df10[row_random, col_names_random_position] <- "1,2"
untidy_tibbles <-
untidy_tibbles %>%
append(list(untidy_df10))
tibbles_listdf <-
tibbles_listdf %>%
bind_rows(tibble(type = "untidy", data = list(untidy_df10),
explanation = paste0("Die Spalte ", col_names_random,
"weißt einen nicht erlaubten Wert auf.")))untidy_tibbles_chosen_nr <- sample(2:nrow(tibbles_listdf), size = 4)
tibbles_chosen <-
tibbles_listdf %>%
mutate(id = row_number(), .before = type) %>%
filter(id == 1 | id %in% untidy_tibbles_chosen_nr) %>%
sample_n(size = 5) %>% # Reihenfolge permutieren
mutate(id2 = paste0("Tabelle ", LETTERS[row_number()])) %>%
mutate(is_correct = ifelse(type == "tidy", TRUE, FALSE))Tabelle A:
gt(tibbles_chosen$data[[1]]) %>% tab_header(title = tibbles_chosen$id2[[1]])| Tabelle A | |||
| group | y | id1 | id2 |
|---|---|---|---|
| 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 1 |
| 1 | 1 | 3 | 2 |
| 2 | 1 | 4 | 2 |
Tabelle B:
gt(tibbles_chosen$data[[2]]) %>% tab_header(title = tibbles_chosen$id2[[2]])| Tabelle B | ||||
| group | y | id1 | id2 | |
|---|---|---|---|---|
| 1 | 10 | 1 | 1 | NA |
| 2 | 20 | 2 | 1 | NA |
| 1 | 30 | 3 | 2 | NA |
| 2 | 40 | 4 | 2 | NA |
Tabelle C:
gt(tibbles_chosen$data[[3]]) %>% tab_header(title = tibbles_chosen$id2[[3]])| Tabelle C | |||
| group | y | id1 | id2 |
|---|---|---|---|
| 1 | NA | 1 | 1 |
| 2 | NA | 2 | 1 |
| 1 | NA | 3 | 2 |
| 2 | NA | 4 | 2 |
Tabelle D:
gt(tibbles_chosen$data[[4]]) %>% tab_header(title = tibbles_chosen$id2[[4]])| Tabelle D | |||
| group | y | id1 | id2 |
|---|---|---|---|
| 1 | 10 | 1 | 1 |
| 2 | 20 | 2 | 1 |
| 1 | 30 | 3 | 2 |
| 2 | 40 | 4 | 2 |
Tabelle E:
gt(tibbles_chosen$data[[5]]) %>% tab_header(title = tibbles_chosen$id2[[5]])| Tabelle E | |||
| group | y | id1 | id2 |
|---|---|---|---|
| 1 | A | 1 | 1 |
| 2 | B | 2 | 1 |
| 1 | C | 3 | 2 |
| 2 | D | 4 | 2 |
4.2 Lösung
Categories:
- tidy
- datawrangling
- schoice