Last updated: 2024-11-24
Checks: 7 0
Knit directory: demor2/
This reproducible R Markdown analysis was created with workflowr (version 1.7.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20241122)
was run prior to running
the code in the R Markdown file. Setting a seed ensures that any results
that rely on randomness, e.g. subsampling or permutations, are
reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 6b55299. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for
the analysis have been committed to Git prior to generating the results
(you can use wflow_publish
or
wflow_git_commit
). workflowr only checks the R Markdown
file, but you know if there are other scripts or data files that it
depends on. Below is the status of the Git repository when the results
were generated:
Ignored files:
Ignored: .DS_Store
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were
made to the R Markdown (analysis/tidyr.Rmd
) and HTML
(docs/tidyr.html
) files. If you’ve configured a remote Git
repository (see ?wflow_git_remote
), click on the hyperlinks
in the table below to view the files as they were in that past version.
File | Version | Author | Date | Message |
---|---|---|---|---|
Rmd | 6b55299 | Chun-Hui Lin | 2024-11-24 | Add forcats- and tidyr-related files. |
Some notes on data tidying from the tidyr
site.
pivot_longer()
names_prefix=
argument: remove matching text form the
start of each variable name.names_sep=
argument: specify positions or regular
expression to break on.names_pattern=
argument: specify regular expression to
split variable name into multiple columns.names_transform=
argument: convert variable type to
integer.values_drop_na=
argument: drop NA
rows if
TRUE
.Regular Expressions
.
: matches any character.?
: 0 or 1.*
: 0 or more.# Numeric data in columns names
billboard %>%
pivot_longer(
cols = tidyselect::starts_with("wk"),
names_to = "week",
names_prefix = "wk",
names_transform = as.integer, # equivalent: readr::parse_number
values_to = "rank",
values_drop_na = TRUE,
)
# A tibble: 5,307 × 5
artist track date.entered week rank
<chr> <chr> <date> <int> <dbl>
1 2 Pac Baby Don't Cry (Keep... 2000-02-26 1 87
2 2 Pac Baby Don't Cry (Keep... 2000-02-26 2 82
3 2 Pac Baby Don't Cry (Keep... 2000-02-26 3 72
4 2 Pac Baby Don't Cry (Keep... 2000-02-26 4 77
5 2 Pac Baby Don't Cry (Keep... 2000-02-26 5 87
6 2 Pac Baby Don't Cry (Keep... 2000-02-26 6 94
7 2 Pac Baby Don't Cry (Keep... 2000-02-26 7 99
8 2Ge+her The Hardest Part Of ... 2000-09-02 1 91
9 2Ge+her The Hardest Part Of ... 2000-09-02 2 87
10 2Ge+her The Hardest Part Of ... 2000-09-02 3 92
# ℹ 5,297 more rows
# Many variables in column names
(who_wide = who %>%
pivot_longer(
cols = new_sp_m014:newrel_f65,
names_to = c("diagnosis", "gender", "age"),
names_pattern = "new_?(.*)_(.)(.*)",
names_transform = list(
gender = as.factor, # equivalent: readr::parse_factor
age = ~ readr::parse_factor(
.x,
levels = c("014", "1524", "2534", "3544", "4554", "5564", "65"),
ordered = TRUE
)
),
values_to = "count",
values_drop_na = TRUE,
))
# A tibble: 76,046 × 8
country iso2 iso3 year diagnosis gender age count
<chr> <chr> <chr> <dbl> <chr> <fct> <ord> <dbl>
1 Afghanistan AF AFG 1997 sp m 014 0
2 Afghanistan AF AFG 1997 sp m 1524 10
3 Afghanistan AF AFG 1997 sp m 2534 6
4 Afghanistan AF AFG 1997 sp m 3544 3
5 Afghanistan AF AFG 1997 sp m 4554 5
6 Afghanistan AF AFG 1997 sp m 5564 2
7 Afghanistan AF AFG 1997 sp m 65 0
8 Afghanistan AF AFG 1997 sp f 014 5
9 Afghanistan AF AFG 1997 sp f 1524 38
10 Afghanistan AF AFG 1997 sp f 2534 36
# ℹ 76,036 more rows
# Multiple observations per row
household %>%
pivot_longer(
cols = !family,
names_to = c(".value", "child"), # part of the column name specify the value being measured
names_sep = "_",
values_drop_na = TRUE
)
# A tibble: 9 × 4
family child dob name
<int> <chr> <date> <chr>
1 1 child1 1998-11-26 Susan
2 1 child2 2000-01-29 Jose
3 2 child1 1996-06-22 Mark
4 3 child1 2002-07-11 Sam
5 3 child2 2004-04-05 Seth
6 4 child1 2004-10-10 Craig
7 4 child2 2009-08-27 Khai
8 5 child1 2000-12-05 Parker
9 5 child2 2005-02-28 Gracie
pivot_wider()
name_glue=
argument: use name_from
columns
to create custom column names.name_expand=
argument: show implicit factor levels if
TRUE
.values_fill=
argument: specify the value filled in with
when missing.values_fn=
argument: specify the function apply to the
value.unused_fn=
argument: summarize the values from the
unused column by specified function.# Capture-recapture data
fish_encounters %>%
pivot_wider(
names_from = station,
values_from = seen,
values_fill = 0
)
# A tibble: 19 × 12
fish Release I80_1 Lisbon Rstr Base_TD BCE BCW BCE2 BCW2 MAE MAW
<fct> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 4842 1 1 1 1 1 1 1 1 1 1 1
2 4843 1 1 1 1 1 1 1 1 1 1 1
3 4844 1 1 1 1 1 1 1 1 1 1 1
4 4845 1 1 1 1 1 0 0 0 0 0 0
5 4847 1 1 1 0 0 0 0 0 0 0 0
6 4848 1 1 1 1 0 0 0 0 0 0 0
7 4849 1 1 0 0 0 0 0 0 0 0 0
8 4850 1 1 0 1 1 1 1 0 0 0 0
9 4851 1 1 0 0 0 0 0 0 0 0 0
10 4854 1 1 0 0 0 0 0 0 0 0 0
11 4855 1 1 1 1 1 0 0 0 0 0 0
12 4857 1 1 1 1 1 1 1 1 1 0 0
13 4858 1 1 1 1 1 1 1 1 1 1 1
14 4859 1 1 1 1 1 0 0 0 0 0 0
15 4861 1 1 1 1 1 1 1 1 1 1 1
16 4862 1 1 1 1 1 1 1 1 1 0 0
17 4863 1 1 0 0 0 0 0 0 0 0 0
18 4864 1 1 0 0 0 0 0 0 0 0 0
19 4865 1 1 1 0 0 0 0 0 0 0 0
# Aggregation
warpbreaks %>%
pivot_wider(
names_from = wool,
values_from = breaks,
values_fn = mean
)
# A tibble: 3 × 3
tension A B
<fct> <dbl> <dbl>
1 L 44.6 28.2
2 M 24 28.8
3 H 24.6 18.8
# Generate column name from multiple variables
who_wide %>%
pivot_wider(
names_from = c(diagnosis, gender, age),
values_from = count,
names_glue = "cnt_{diagnosis}_{gender}_{age}"
)
# A tibble: 3,484 × 60
country iso2 iso3 year cnt_sp_m_014 cnt_sp_m_1524 cnt_sp_m_2534
<chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 Afghanistan AF AFG 1997 0 10 6
2 Afghanistan AF AFG 1998 30 129 128
3 Afghanistan AF AFG 1999 8 55 55
4 Afghanistan AF AFG 2000 52 228 183
5 Afghanistan AF AFG 2001 129 379 349
6 Afghanistan AF AFG 2002 90 476 481
7 Afghanistan AF AFG 2003 127 511 436
8 Afghanistan AF AFG 2004 139 537 568
9 Afghanistan AF AFG 2005 151 606 560
10 Afghanistan AF AFG 2006 193 837 791
# ℹ 3,474 more rows
# ℹ 53 more variables: cnt_sp_m_3544 <dbl>, cnt_sp_m_4554 <dbl>,
# cnt_sp_m_5564 <dbl>, cnt_sp_m_65 <dbl>, cnt_sp_f_014 <dbl>,
# cnt_sp_f_1524 <dbl>, cnt_sp_f_2534 <dbl>, cnt_sp_f_3544 <dbl>,
# cnt_sp_f_4554 <dbl>, cnt_sp_f_5564 <dbl>, cnt_sp_f_65 <dbl>,
# cnt_sn_m_014 <dbl>, cnt_sn_m_1524 <dbl>, cnt_sn_m_2534 <dbl>,
# cnt_sn_m_3544 <dbl>, cnt_sn_m_4554 <dbl>, cnt_sn_m_5564 <dbl>, …
# Tidy census
daily %>%
pivot_wider(
names_from = day,
values_from = value,
names_expand = TRUE,
values_fill = 0
)
# A tibble: 4 × 5
...1 Fri Mon Thu Tue
<dbl> <dbl> <dbl> <dbl> <dbl>
1 1 0 0 0 2
2 2 0 0 3 0
3 3 1 0 0 0
4 4 0 5 0 0
# Unused columns
updates %>%
pivot_wider(
id_cols = county,
names_from = system,
values_from = value,
unused_fn = list(date = max)
)
# A tibble: 2 × 5
county A B C date
<chr> <dbl> <dbl> <dbl> <date>
1 Wake 3.2 4 5.5 2020-01-03
2 Guilford 2 NA 1.2 2020-01-04
sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS 15.1.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: America/Detroit
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4
[5] purrr_1.0.2 readr_2.1.5 tibble_3.2.1 ggplot2_3.5.1
[9] tidyverse_2.0.0 tidyr_1.3.1 workflowr_1.7.1
loaded via a namespace (and not attached):
[1] sass_0.4.9 utf8_1.2.4 generics_0.1.3 stringi_1.8.4
[5] hms_1.1.3 digest_0.6.37 magrittr_2.0.3 timechange_0.3.0
[9] evaluate_1.0.0 grid_4.4.0 fastmap_1.2.0 rprojroot_2.0.4
[13] jsonlite_1.8.9 processx_3.8.4 whisker_0.4.1 ps_1.7.6
[17] promises_1.3.0 httr_1.4.7 fansi_1.0.6 scales_1.3.0
[21] jquerylib_0.1.4 cli_3.6.3 crayon_1.5.2 rlang_1.1.4
[25] bit64_4.0.5 munsell_0.5.1 withr_3.0.1 cachem_1.1.0
[29] yaml_2.3.10 parallel_4.4.0 tools_4.4.0 tzdb_0.4.0
[33] colorspace_2.1-1 httpuv_1.6.15 vctrs_0.6.5 R6_2.5.1
[37] lifecycle_1.0.4 git2r_0.33.0 bit_4.0.5 fs_1.6.4
[41] vroom_1.6.5 pkgconfig_2.0.3 callr_3.7.6 pillar_1.9.0
[45] bslib_0.8.0 later_1.3.2 gtable_0.3.5 glue_1.7.0
[49] Rcpp_1.0.13 xfun_0.47 tidyselect_1.2.1 rstudioapi_0.16.0
[53] knitr_1.48 htmltools_0.5.8.1 rmarkdown_2.28 compiler_4.4.0
[57] getPass_0.2-4