4  tidy1

tidy
datawrangling
schoice
Veröffentlichungsdatum

2. Februar 2023

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.
# prepare some helper constants:
rows_n <- 4
group_n <- 2
constant_factor <- 10
tidy_df <- 
  tibble(
    group = rep(c(1:group_n), times = rows_n/group_n),
    y = (1:rows_n)*constant_factor
  ) %>% 
  mutate(id1 = row_number()) %>% 
  group_by(group) %>% 
  mutate(id2 = row_number()) %>% 
  ungroup()
# more helper constants:
cols_n <-
  ncol(tidy_df)

col_random <- sample(1:cols_n, 1)
row_random <- sample(1:nrow(tidy_df), 1)
col_names <- names(tidy_df)
col_names_random <- sample(col_names, 1)
col_names_random_position <- which(names(tidy_df) == col_names_random)
untidy_tibbles <- list()
tibbles_listdf <- tibble(type = "tidy",
                         data = list(tidy_df),
                         explanation = "Das ist ein 'tidy Tibble'.")
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] <- NA
empty_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_df4 <- 
tidy_df %>% 
  mutate(!!col_names_random := NA)

untidy_tibbles <- 
untidy_tibbles %>% 
  append(list(untidy_df4))

tibbles_listdf <- 
tibbles_listdf %>% 
  bind_rows(tibble(type = "untidy", data = list(untidy_df4),
                   explanation = "In einem Tidy-Tibble darf keine Spalte nur aus `NA` bestehen."))
untidy_df5 <-
  tidy_df %>% 
  mutate(!!col_names_random := LETTERS[1:nrow(.)])

untidy_tibbles <- 
untidy_tibbles %>% 
  append(list(untidy_df5))


tibbles_listdf <- 
tibbles_listdf %>% 
  bind_rows(tibble(type = "untidy", data = list(untidy_df5),
                   explanation = "Alle Werte sollen numerisch sein"))
untidy_df6 <-
  tidy_df %>% 
  mutate(!!col_names_random := 1)

untidy_tibbles <- 
untidy_tibbles %>% 
  append(list(untidy_df6))


tibbles_listdf <- 
tibbles_listdf %>% 
  bind_rows(tibble(type = "untidy", data = list(untidy_df6),
                   explanation = "Variablen sollen nicht konstant sein."))
untidy_df7 <-
  tidy_df %>% 
  mutate(!!col_names_random := na_if(id1, row_random))

untidy_tibbles <- 
untidy_tibbles %>% 
  append(list(untidy_df7))



tibbles_listdf <- 
tibbles_listdf %>% 
  bind_rows(tibble(type = "untidy", data = list(untidy_df7),
                   explanation = "Fehlende Werte sind nicht erlaubt"))
untidy_df8 <-
  tidy_df %>% 
  mutate(!!col_names_random := "1,2")

untidy_tibbles <- 
untidy_tibbles %>% 
  append(list(untidy_df8))



tibbles_listdf <- 
tibbles_listdf %>% 
  bind_rows(tibble(type = "untidy", data = list(untidy_df8),
                   explanation = "In einer Zelle darf nur ein (numerischer) Wert stehen."))
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