From 2e8e9f5f93807f5642cdebd2029ef872e59f3bfc Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Tue, 5 Nov 2024 15:28:34 +0100 Subject: [PATCH] Initial version of cheat sheet. --- config.toml | 5 +++ content/cheatsheet.md | 97 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 content/cheatsheet.md diff --git a/config.toml b/config.toml index b6c7f36..e00c09c 100644 --- a/config.toml +++ b/config.toml @@ -134,3 +134,8 @@ expiryDate = ["expiryDate"] name = "Impressum" url = "impressum/" weight = 30 + + [[menu.main]] + name = "Cheat sheet" + url = "cheatsheet/" + weight = 15 diff --git a/content/cheatsheet.md b/content/cheatsheet.md new file mode 100644 index 0000000..37d5803 --- /dev/null +++ b/content/cheatsheet.md @@ -0,0 +1,97 @@ +--- +title: bovender's personal cheat sheet +description: > + On this page, I collect bits and pieces of information that I + never seem to be able to memorize. This stuff is helpful for me + and maybe it is helpful for you, too, internet wanderer. +date: 2024-11-05T08:00:00+0100 +draft: false +--- +{{< git-info >}} +{{< param "description" >}} + +## R + +### Print R objects so that they can be re-generated from code + +```r +# dput takes an R object +dput(x, file = "", control = c("keepNA", "keepInteger", "niceNames", "showAttributes")) +dget(file, keep.source = FALSE) + +# dump takes a list of R object names +dump(list, file = "dumpdata.R", append = FALSE, control = "all", envir = parent.frame(), evaluate = TRUE) +``` + +Example: + +``` r +library(dplyr) +my_data <- + starwars |> + select(species, homeworld) +dput(my_data) +#> structure(list(species = c("Human", "Droid", "Droid", "Human", +#> "Human", "Human", "Human", "Droid", "Human", "Human", "Human", +#> "Human", "Wookiee", "Human", "Rodian", "Hutt", "Human", NA, "Yoda's species", +#> "Human", "Human", "Droid", "Trandoshan", "Human", "Human", "Mon Calamari", +#> "Human", "Human", "Ewok", "Sullustan", "Human", "Neimodian", +#> "Human", "Human", "Gungan", "Gungan", "Gungan", "Human", "Toydarian", +#> "Dug", "Human", "Human", "Zabrak", "Twi'lek", "Twi'lek", "Aleena", +#> "Vulptereen", "Xexto", "Toong", "Human", "Cerean", "Nautolan", +#> "Zabrak", "Tholothian", "Iktotchi", "Quermian", "Kel Dor", "Chagrian", +#> NA, NA, "Human", "Geonosian", "Mirialan", "Mirialan", "Human", +#> "Human", "Human", "Human", "Clawdite", "Besalisk", "Kaminoan", +#> "Kaminoan", "Human", "Droid", "Skakoan", "Muun", "Togruta", "Kaleesh", +#> "Wookiee", "Human", NA, "Pau'an", "Human", "Human", "Human", +#> "Droid", "Human"), homeworld = c("Tatooine", "Tatooine", "Naboo", +#> "Tatooine", "Alderaan", "Tatooine", "Tatooine", "Tatooine", "Tatooine", +#> "Stewjon", "Tatooine", "Eriadu", "Kashyyyk", "Corellia", "Rodia", +#> "Nal Hutta", "Corellia", "Bestine IV", NA, "Naboo", "Kamino", +#> NA, "Trandosha", "Socorro", "Bespin", "Mon Cala", "Chandrila", +#> NA, "Endor", "Sullust", NA, "Cato Neimoidia", "Coruscant", "Naboo", +#> "Naboo", "Naboo", "Naboo", "Naboo", "Toydaria", "Malastare", +#> "Naboo", "Tatooine", "Dathomir", "Ryloth", "Ryloth", "Aleen Minor", +#> "Vulpter", "Troiken", "Tund", "Haruun Kal", "Cerea", "Glee Anselm", +#> "Iridonia", "Coruscant", "Iktotch", "Quermia", "Dorin", "Champala", +#> "Naboo", "Naboo", "Tatooine", "Geonosis", "Mirial", "Mirial", +#> "Naboo", "Serenno", "Alderaan", "Concord Dawn", "Zolan", "Ojom", +#> "Kamino", "Kamino", "Coruscant", NA, "Skako", "Muunilinst", "Shili", +#> "Kalee", "Kashyyyk", "Alderaan", "Umbara", "Utapau", NA, NA, +#> NA, NA, NA)), row.names = c(NA, -87L), class = c("tbl_df", "tbl", +#> "data.frame")) +``` + +Created on 2024-11-05 with [reprex v2.1.1](https://reprex.tidyverse.org) + +### Calculate percentages of subgroups + +Principle: `Group` the dataset and `summarize` by counting (`n()`), drop the last group +while doing so, then `mutate` the dataset by adding the count divided by the sum of counts. + +Hint to better understand the example: Look at the sexes in black-eyed subjects. + +``` r +library(dplyr) +starwars |> + group_by(eye_color, gender) |> + summarize(n = n(), .groups = "drop_last") |> + mutate(p = n/sum(n)) +#> # A tibble: 23 × 4 +#> # Groups: eye_color [15] +#> eye_color gender n p +#> +#> 1 black feminine 2 0.2 +#> 2 black masculine 8 0.8 +#> 3 blue feminine 6 0.316 +#> 4 blue masculine 12 0.632 +#> 5 blue 1 0.0526 +#> 6 blue-gray masculine 1 1 +#> 7 brown feminine 4 0.190 +#> 8 brown masculine 15 0.714 +#> 9 brown 2 0.0952 +#> 10 dark masculine 1 1 +#> # ℹ 13 more rows +``` + +Created on 2024-11-05 with [reprex v2.1.1](https://reprex.tidyverse.org)