Consider the following MWE:
using DataFrames
df_str = """
mdate type yesno workerid
1/1/2020 pcr no 1
1/1/2020 molecular yes 1
1/1/2020 pcr yes 2
1/1/2020 molecular yes 2
1/1/2020 antigen yes 2
1/1/2020 molecular no 3
1/2/2020 pcr no 1
1/2/2020 molecular yes 1
1/2/2020 antigen yes 1
1/2/2020 molecular yes 2
1/2/2020 antigen yes 3
1/2/2020 molecular no 3
1/2/2020 pcr yes 3
"""
df = CSV.File(IOBuffer(df_str)) |> DataFrame!
I would like to widen this table, i.e.
# pivot
widedf = unstack(df, :type, :yesno)
# pivot
widedf = unstack(df, :type, :yesno)
pretty_table(widedf, crop=:none)
┌──────────┬──────────┬────────────────────────┬────────────────────────┬────────────────────────┐
│ mdate │ workerid │ antigen │ molecular │ pcr │
│ String │ Int64 │ Union{Missing, String} │ Union{Missing, String} │ Union{Missing, String} │
├──────────┼──────────┼────────────────────────┼────────────────────────┼────────────────────────┤
│ 1/1/2020 │ 1 │ missing │ yes │ no │
│ 1/1/2020 │ 2 │ yes │ yes │ yes │
│ 1/1/2020 │ 3 │ missing │ no │ missing │
│ 1/2/2020 │ 1 │ yes │ yes │ no │
│ 1/2/2020 │ 2 │ missing │ yes │ missing │
│ 1/2/2020 │ 3 │ yes │ no │ yes │
but I would like to fix a few things here:
- I would like to change the column types to purely
String
. - I would like to replace the missing data with
no
.