# prepare some helper constants:
rows_n <- 4
group_n <- 2
constant_factor <- 104 tidy1
tidy
datawrangling
schoice
paper
Schlüsselwörter
Aufgaben, Statistik, Prognose, Modellierung, R, Datenanalyse, Regression
5 Question
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 | |||
| id1 | id2 | 1 | 2 |
|---|---|---|---|
| 1 | 1 | 10 | NA |
| 2 | 1 | NA | 20 |
| 3 | 2 | 30 | NA |
| 4 | 2 | NA | 40 |
Tabelle B:
gt(tibbles_chosen$data[[2]]) %>% tab_header(title = tibbles_chosen$id2[[2]])| Tabelle B | |||
| group | y | id1 | id2 |
|---|---|---|---|
| 1 | 10 | A | 1 |
| 2 | 20 | B | 1 |
| 1 | 30 | C | 2 |
| 2 | 40 | D | 2 |
Tabelle C:
gt(tibbles_chosen$data[[3]]) %>% tab_header(title = tibbles_chosen$id2[[3]])| Tabelle C | |||
| group | y | id1 | id2 |
|---|---|---|---|
| 1 | 10 | 1 | 1 |
| 2 | 20 | 1 | 1 |
| 1 | 30 | 1 | 2 |
| 2 | 40 | 1 | 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 |
|---|---|---|---|
| 2 | 40 | 4 | 2 |
| NA | NA | NA | NA |
| 2 | 20 | 2 | 1 |
| 1 | 30 | 3 | 2 |
| 1 | 10 | 1 | 1 |
6 Solution
7 Meta-information
exname: tidy1 extype: schoice exsolution: 00010 exshuffle: FALSE
Categories:
- tidy
- datawrangling
- schoice