Skip to content

Commit

Permalink
Move the preallocation into the subset
Browse files Browse the repository at this point in the history
  • Loading branch information
delosh653 committed Aug 14, 2023
1 parent 3c4d7bb commit 0b1445c
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions R/infants_clean.R
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,6 @@ cleanbatch_infants <- function(data.df,
testing <- TRUE

while (testing & nrow(df) > 1){

# sort df since it got reordered with keys
df <- df[order(agedays),]

Expand Down Expand Up @@ -1394,8 +1393,6 @@ cleanbatch_infants <- function(data.df,
# 17F
# merge with WHO
# add the column name we want to grab
df[, who_mindiff_ht := NA]
df[, who_maxdiff_ht := NA]
for (i in unique(df$whoinc.age.ht[!is.na(df$whoinc.age.ht)])){
cn <- paste0("whoinc.", i, ".ht")
df[, who_mindiff_ht :=
Expand All @@ -1404,6 +1401,11 @@ cleanbatch_infants <- function(data.df,
df[, who_maxdiff_ht :=
as.numeric(who.ht.vel[whoagegrp.ht == i & sex == df$sex[1], get(cn)])]
}
# if there are none, preallocate for ease
if (length(unique(df$whoinc.age.ht[!is.na(df$whoinc.age.ht)])) < 1){
df[, who_mindiff_ht := NA_real_]
df[, who_maxdiff_ht := NA_real_]
}
df[, who_mindiff_ht := as.numeric(who_mindiff_ht)]
df[, who_mindiff_ht := as.numeric(who_maxdiff_ht)]

Expand Down Expand Up @@ -1436,11 +1438,12 @@ cleanbatch_infants <- function(data.df,
df[d_agedays >= 107 & d_agedays < 153, whoinc.age.hc := 4]
df[d_agedays >= 153 & d_agedays < 199, whoinc.age.hc := 6]

# update the edge intervals to missing
df[d_agedays < 46 | d_agedays > 199, whoinc.age.hc := NA]

# 17K
# merge with WHO
# add the column name we want to grab
df[, who_mindiff_hc := NA]
df[, who_maxdiff_hc := NA]
for (i in unique(df$whoinc.age.hc[!is.na(df$whoinc.age.hc)])){
cn <- paste0("whoinc.", i, ".ht")
df[, who_mindiff_hc :=
Expand All @@ -1449,6 +1452,11 @@ cleanbatch_infants <- function(data.df,
df[, who_maxdiff_hc :=
as.numeric(who.hc.vel[whoagegrp.ht == i & sex == df$sex[1], get(cn)])]
}
# if there are none, preallocate for ease
if (length(unique(df$whoinc.age.hc[!is.na(df$whoinc.age.hc)])) < 1){
df[, who_mindiff_hc := NA_real_]
df[, who_maxdiff_hc := NA_real_]
}
df[, who_mindiff_hc := as.numeric(who_mindiff_hc)]
df[, who_mindiff_hc := as.numeric(who_maxdiff_hc)]

Expand Down

0 comments on commit 0b1445c

Please sign in to comment.