-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.jl
60 lines (57 loc) · 2.06 KB
/
functions.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
## ------------------------------------------------------------------------
##
## Script name: functions.jl
## Purpose: Functions for cleaning the raw data
## Author: Yanwen Wang
## Date Created: 2024-12-08
## Email: [email protected]
##
## ------------------------------------------------------------------------
##
## Notes:
##
## ------------------------------------------------------------------------
# Data cleaning -----------------------------------------------------------
#=
Function to create matches for parents, parents-in-law, or grandparents
within each household with multiple pairs
=#
function create_parent_matches(sdf)
pernums = sort(sdf.pernum)
num_pairs = div(length(pernums), 2)
hhid = sdf.hhid[1]
maryr = sdf.maryr[1]
matches = DataFrame(hhid=String[], pernum=Int[], sploc=Int[])
for i in 1:num_pairs
# Pair pernums[2i - 1] with pernums[2i]
gp1 = pernums[2i-1]
gp2 = pernums[2i]
# Assign sploc for both parents
push!(matches, (hhid=hhid, pernum=gp1, sploc=gp2))
push!(matches, (hhid=hhid, pernum=gp2, sploc=gp1))
end
# If there's an unmatched grandparent (odd number), their 'sploc' remains missing
return matches
end
#=
Function to create matches of children and children-in-law
within each household with multiple pairs
=#
function create_children_matches(sdf)
# Extract `pernum` of children and children-in-law
children = sort(sdf[sdf.role.=="child", :pernum])
children_in_law = sort(sdf[sdf.role.=="child_in_law", :pernum])
num_pairs = min(length(children), length(children_in_law))
hhid = sdf.hhid[1]
maryr = sdf.maryr[1]
matches = DataFrame(hhid=String[], pernum=Int[], sploc=Int[])
for i in 1:num_pairs
child_pernum = children[i]
child_in_law_pernum = children_in_law[i]
# Assign sploc for child
push!(matches, (hhid=hhid, pernum=child_pernum, sploc=child_in_law_pernum))
# Assign sploc for child-in-law
push!(matches, (hhid=hhid, pernum=child_in_law_pernum, sploc=child_pernum))
end
return matches
end