Skip to content

Commit d925665

Browse files
authored
Merge pull request #6 from TidierOrg/dbupdates
syntax updates for DB
2 parents dd0dd18 + 1b17edb commit d925665

2 files changed

Lines changed: 26 additions & 28 deletions

File tree

from-dataframes-to-databases.jl

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,16 @@ end
149149
@summarize(max_birthdate = maximum(birthdate),
150150
max_deathdate = maximum(deathdate))
151151
@collect()
152-
@pivot_longer()
152+
@pivot_longer(cols = everything())
153153
end
154154

155155
# ╔═╡ 1f9d1d3d-6132-4cfe-a8ab-132aba378f96
156156
todays_date = @chain begin
157-
db_table(connect(duckdb()), "data/patients.csv")
157+
patients
158158
@summarize(max_birthdate = maximum(birthdate),
159159
max_deathdate = maximum(deathdate))
160160
@collect()
161-
@pivot_longer()
161+
@pivot_longer(cols = everything())
162162
@filter(value == maximum(value))
163163
@pull(value)
164164
_[1] # 1
@@ -204,32 +204,32 @@ patients_age = @eval @chain begin
204204
end
205205

206206
# ╔═╡ 005330a2-e7b1-4381-9d50-453ba1d2ce4e
207-
@chain t(patients_age) begin
207+
@chain patients_age begin
208208
@mutate(age_category = if_else(age >= 18, "Adult", "Child"))
209209
@count(age_category)
210210
@collect()
211211
end
212212

213213
# ╔═╡ 5d2390fb-c618-4d19-963b-9b2601021c80
214-
@chain t(patients_age) begin
214+
@chain patients_age begin
215215
@mutate(age_category =
216-
case_when(age >= 75, "Older adult",
217-
age >= 18, "Adult",
218-
age >= 4, "Child",
219-
true, "Infant"))
216+
case_when(age >= 75 => "Older adult",
217+
age >= 18 => "Adult",
218+
age >= 4 => "Child",
219+
true => "Infant"))
220220
@count(age_category)
221221
@collect()
222222
end
223223

224224
# ╔═╡ c8ccb7ec-ae75-4dbb-9448-0bfe853b5268
225-
@chain t(patients_age) begin
225+
@chain patients_age begin
226226
@mutate(age_category =
227-
case_when(age >= 75 && gender == "F", "Older adult female",
228-
age >= 75 && gender == "M", "Older adult male",
229-
age >= 18 && gender == "F", "Adult female",
230-
age >= 18 && gender == "M", "Adult male",
231-
age >= 4, "Child",
232-
true, "Infant"))
227+
case_when(age >= 75 && gender == "F" => "Older adult female",
228+
age >= 75 && gender == "M" => "Older adult male",
229+
age >= 18 && gender == "F" => "Adult female",
230+
age >= 18 && gender == "M" => "Adult male",
231+
age >= 4 => "Child",
232+
true => "Infant"))
233233
@count(age_category)
234234
@collect()
235235
end
@@ -241,34 +241,32 @@ patients = db_table(connect(duckdb()), "data/patients.csv");
241241
meds = db_table(connect(duckdb()), "data/medications.csv");
242242

243243
# ╔═╡ 215ed9ca-6698-4f00-ac25-04fc08470981
244-
@chain t(patients) @collect()
244+
@chain patients @collect()
245245

246246
# ╔═╡ 2b7d151e-af7c-49ab-9b64-6b3d922c9312
247-
@chain t(meds) @collect()
247+
@chain meds @collect()
248248

249249
# ╔═╡ 917616d8-bc68-450f-bb72-0571985643af
250-
@chain t(meds) begin
250+
@chain meds begin
251251
@filter(!ismissing(start), ismissing(stop)) # med is still active
252252
@count(patient) # count unique rows at the patient level, save result to `n`
253-
@arrange(desc(count)) # arrange in descending order of count
253+
@arrange(desc(n)) # arrange in descending order of count
254254
@collect()
255255
end
256256

257257
# ╔═╡ b256e86c-c091-4125-bd33-867908b19b0a
258258
meds_clean_count =
259-
@chain t(meds) begin
259+
@chain meds begin
260260
@filter(!ismissing(start), ismissing(stop)) # med is still active
261261
@count(patient) # count unique rows at the patient level, save result to `n`
262-
@arrange(desc(count)) # arrange in descending order of count
262+
@arrange(desc(n)) # arrange in descending order of count
263263
end
264264

265265
# ╔═╡ 662ee3aa-1239-419d-b0a4-516ef8b47a15
266266
@chain begin
267-
@left_join(t(patients), t(meds_clean_count), id = patient)
268-
@mutate(num_meds = replace_missing(count, 0))
269-
@summarize(mean_num_meds = mean(num_meds),
270-
min_num_meds = minimum(num_meds),
271-
max_num_meds = maximum(num_meds))
267+
@left_join(patients, meds_clean_count, id = patient)
268+
@mutate(num_meds = replace_missing(n, 0))
269+
@summarize(across(num_meds, (mean, minimum, maximum)))
272270
@collect()
273271
end
274272

recoding-data.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ md"""
351351
352352
If you think of `if_else` and `case_when` as the workhorses of recoding data, `TidierCats.jl` provides a set of bespoke functions for achieving many of the same goals as our two handy go-to functions. Fancy, fancy!
353353
354-
Had we wanted to chop age into categories, we could've achieved this using TidierCats `cut` function. THe `extend = true` argument ensures that the values higher than the greatest threshold (i.e., those older than 75) are lumped in with the highest category (in this case, "Older adult").
354+
Had we wanted to chop age into categories, we could've achieved this using CategoricalArrays `cut` function (reexported by TidierCats). THe `extend = true` argument ensures that the values higher than the greatest threshold (i.e., those older than 75) are lumped in with the highest category (in this case, "Older adult").
355355
356356
## `cut` for cutting continuous values into categories
357357
"""

0 commit comments

Comments
 (0)