Instructions

rename, retype 3 variables

retype a variable that is incorrect, or reclassify one to be incorrect

calculate sum of ‘NAs’ in 10 columns

Identify positions of ‘NAs’ in 1 column

Recode a categorical variable

Produce a frequency table for the original and recoded variables side-by-side

create a derived variable

export data set as .csv or .xlsx

Load & filter dataset

# load the Stata file
dat <- read_dta("//qpa1/Data/Intern/Spokane_PRAMS_phase8_2016_2022.dta")

# checking the column type for filtering
str(dat$cnty_res)
##  num [1:8344] 88 32 88 88 88 32 88 32 88 88 ...
##  - attr(*, "format.stata")= chr "%12.0g"
# filter to Spokane County, which is 32
dat <- dat |> 
  filter(cnty_res == 32)

Inspect dataset

Dat has 463 rows across 682 columns. For the most part, columns have a small percentage of NA responses. Some columns have a higher level of NA responses than others, like the dat$momlbs column which is missing 9% of responses.

Resource: https://stackoverflow.com/questions/tagged/r?tab=Newest

’str(dat$momlbs) unique() table()

sum(is.na()) which(is.na())’

library(haven) Spokane_PRAMS_phase8_2016_2022 <- read_dta(“//qpa1/Data/Intern/Spokane_PRAMS_phase8_2016_2022.dta”) View(Spokane_PRAMS_phase8_2016_2022)

Practice

Calculate Sum of NA values

# Sum NAs of moms who smoke
sum(is.na(dat$momsmoke))
## [1] 11
#Number of moms who smoke w/o NA
sum(!is.na(dat$momsmoke))
## [1] 452
#Sum NA Breastfed
sum(is.na(dat$brstfed))
## [1] 8
#Sum NA ever breastfed
sum(is.na(dat$bf5ever))
## [1] 16
#Sum NA birth defects
sum(is.na(dat$defect))
## [1] 0
#Sum NA Infertility Treatment
sum(is.na(dat$infer_tr))
## [1] 2
#Sum NA BMI by Group
sum(is.na(dat$mom_bmig_qx_rev))
## [1] 8
# Sum NA Diabetes
sum(is.na(dat$bpg_diab8))
## [1] 6
#sum NA High BP
sum(is.na(dat$bpg_hbp8))
## [1] 6
#Sum NA received flu shot before delivery
sum(is.na(dat$flupreg))
## [1] 6
# Sum NA tdap shot while pregnant
sum(is.na(dat$pg_tdap8))
## [1] 32
#tdap values that contain 'NA'
which(is.na(dat$pg_tdap8))
##  [1]  10  28  48  83  95  99 106 118 146 148 153 157 163 173 219 220 232 249 302
## [20] 306 309 321 345 368 369 371 377 379 432 434 446 458
# Table of Mom's Weight Gain
table(dat$momlbs)
## 
##  0  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 
## 16  4  2  2  1  3  4  2  5  3  8  1  4 10  7  3  7  4  7  6 14  5  7 10  6 21 
## 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 
## 11 11  8  3 37 15  7 12  7 18  5  6 13  6 12  4  6  3 11  8  3  6  5  5  3  2 
## 52 53 54 55 56 57 59 60 61 63 64 66 68 70 73 75 76 80 85 93 
##  1  4  2  2  1  1  3  4  2  1  1  2  1  1  1  1  1  1  1  1

Rename Variables

#Flu shot table, rename rows
flu_table = table(dat$flupreg)
 row.names(flu_table) <- c("no","before","during")
#Print Flu Table
 flu_table
## 
##     no before during 
##    229     57    171
#Infant Birth Month 
table(dat$idob_mth)
## 
##  1  2  3  4  5  6  7  8  9 10 11 12 
## 36 38 44 38 48 38 38 45 30 36 35 31
#Rename from number to month 
birth_mth = table(dat$idob_mth)
  row.names(birth_mth) <-c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
#Print Infant Birth Month Table
birth_mth
## 
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 
##  36  38  44  38  48  38  38  45  30  36  35  31
#Pregnancy Intention
table(dat$pgint3)
## 
##   0   1   2 
##  99 271  82
intention_table = table(dat$pgint3)
 row.names(intention_table) <- c("Unintended", "Intended", "Unsure")

 #Print Intention Table
intention_table
## 
## Unintended   Intended     Unsure 
##         99        271         82
#Table Baby's Sex
dat$sex_num
##   [1] 1 1 2 2 1 1 1 2 2 1 1 2 2 2 2 1 2 1 1 2 1 1 2 1 2 1 2 2 2 1 2 2 2 2 2 1 1
##  [38] 1 2 2 1 1 2 1 1 2 2 1 2 2 1 2 1 1 2 1 1 1 2 2 1 1 1 2 1 1 2 1 2 1 2 2 1 1
##  [75] 2 1 2 2 1 1 2 2 2 1 2 2 2 1 1 2 2 1 1 1 1 1 2 2 2 1 2 2 2 2 2 1 2 2 2 1 2
## [112] 1 2 1 2 2 2 1 2 2 1 2 2 2 1 2 1 1 2 1 1 2 1 2 2 1 2 2 2 1 1 1 2 1 1 1 1 1
## [149] 1 2 2 2 2 2 2 2 2 1 2 2 1 1 1 2 1 2 1 1 1 2 2 1 1 2 2 2 2 2 2 2 2 1 1 2 2
## [186] 1 2 1 1 2 2 1 1 1 2 1 2 1 2 1 2 1 1 1 2 2 2 2 1 2 2 1 2 1 2 2 2 2 2 1 2 1
## [223] 2 1 1 2 2 2 1 1 2 1 2 1 2 1 1 1 1 2 2 2 1 2 2 1 2 2 2 1 2 1 2 2 1 1 2 2 2
## [260] 1 2 2 1 1 1 2 1 2 1 2 1 1 2 2 1 2 2 1 2 2 1 1 1 1 2 1 1 1 2 1 1 1 1 2 1 2
## [297] 2 2 1 1 2 1 1 2 1 1 1 1 2 2 2 2 1 1 1 2 1 2 2 2 1 1 1 1 1 1 2 1 1 1 1 1 2
## [334] 2 1 2 2 2 1 1 2 1 1 2 1 2 2 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 1 2 2 1
## [371] 2 1 1 2 2 2 1 1 1 2 1 2 1 1 2 1 1 2 2 2 2 2 1 2 2 2 1 2 2 1 1 1 1 1 2 1 1
## [408] 1 1 2 1 1 2 2 2 1 1 2 2 1 1 2 1 1 1 2 1 1 1 1 1 2 1 1 2 2 1 2 1 2 1 1 2 1
## [445] 1 2 1 1 2 2 1 1 1 1 1 1 1 1 2 2 1 1 2
## attr(,"label")
## [1] "Infant sex - numeric"
## attr(,"format.stata")
## [1] "%12.0g"
table(dat$sex_num)
## 
##   1   2 
## 240 223
bby_sex = table(dat$sex_num)
 row.names(bby_sex) <- c("Male", "Female")
#Print Baby's Sex as Table, updated labels 
bby_sex
## 
##   Male Female 
##    240    223
#Changing Variable Type
glimpse(dat$sex)
##  chr [1:463] "M" "M" "F" "F" "M" "M" "M" "F" "F" "M" "M" "F" "F" "F" "F" ...
##  - attr(*, "label")= chr "Sex"
##  - attr(*, "format.stata")= chr "%2s"
class(dat$sex)
## [1] "character"
table(dat$sex)
## 
##   F   M 
## 223 240
sex_ordered <- (as.ordered(dat$sex))

#Print Orderd Sex Table
sex_ordered
##   [1] M M F F M M M F F M M F F F F M F M M F M M F M F M F F F M F F F F F M M
##  [38] M F F M M F M M F F M F F M F M M F M M M F F M M M F M M F M F M F F M M
##  [75] F M F F M M F F F M F F F M M F F M M M M M F F F M F F F F F M F F F M F
## [112] M F M F F F M F F M F F F M F M M F M M F M F F M F F F M M M F M M M M M
## [149] M F F F F F F F F M F F M M M F M F M M M F F M M F F F F F F F F M M F F
## [186] M F M M F F M M M F M F M F M F M M M F F F F M F F M F M F F F F F M F M
## [223] F M M F F F M M F M F M F M M M M F F F M F F M F F F M F M F F M M F F F
## [260] M F F M M M F M F M F M M F F M F F M F F M M M M F M M M F M M M M F M F
## [297] F F M M F M M F M M M M F F F F M M M F M F F F M M M M M M F M M M M M F
## [334] F M F F F M M F M M F M F F M M M F M M M M M M M M M M M F M F F M F F M
## [371] F M M F F F M M M F M F M M F M M F F F F F M F F F M F F M M M M M F M M
## [408] M M F M M F F F M M F F M M F M M M F M M M M M F M M F F M F M F M M F M
## [445] M F M M F F M M M M M M M M F F M M F
## Levels: F < M
#Side by side
table(dat$sex, dat$sex_num)
##    
##       1   2
##   F   0 223
##   M 240   0

Derive New Variable

#Baby Weight Range
range(dat$birth_weight_grams)
## [1] 1191 5330
#Average Birth Weight (Grams)
avg_weight <- mean(dat$birth_weight_grams)
avg_weight
## [1] 3294.382
#Average Birth Weight of Male Babies
avg_wt_male <- mean(dat$birth_weight_grams, dat$sex_num [1])
avg_wt_male
## [1] 3317

Recode States

library(dplyr)

dat$mother_birthplace_region <- case_when(
  dat$mother_birthplace_state %in% c(
    "CONNECTICUT","MAINE","MASSACHUSETTS","NEW HAMPSHIRE",
    "RHODE ISLAND","VERMONT","NEW JERSEY","NEW YORK","PENNSYLVANIA"
  ) ~ "Northeast",

  dat$mother_birthplace_state %in% c(
    "ILLINOIS","INDIANA","MICHIGAN","OHIO","WISCONSIN",
    "IOWA","KANSAS","MINNESOTA","MISSOURI","NEBRASKA",
    "NORTH DAKOTA","SOUTH DAKOTA"
  ) ~ "Midwest",

  dat$mother_birthplace_state %in% c(
    "DELAWARE","DISTRICT OF COLUMBIA","FLORIDA","GEORGIA",
    "MARYLAND","NORTH CAROLINA","SOUTH CAROLINA","VIRGINIA",
    "WEST VIRGINIA","ALABAMA","KENTUCKY","MISSISSIPPI",
    "TENNESSEE","ARKANSAS","LOUISIANA","OKLAHOMA","TEXAS"
  ) ~ "South",

  dat$mother_birthplace_state %in% c(
    "ARIZONA","COLORADO","IDAHO","MONTANA","NEVADA",
    "NEW MEXICO","UTAH","WYOMING","ALASKA","CALIFORNIA",
    "HAWAII","OREGON","WASHINGTON"
  ) ~ "West",

  TRUE ~ NA_character_
)

table(dat$mother_birthplace_region, useNA = "ifany")
## 
##   Midwest Northeast     South      West      <NA> 
##        16         5        18       296       128

Side by Side Frequency Table

table(dat$mother_birthplace_region, dat$mother_birthplace_state)
##            
##                 ALASKA ARIZONA ARKANSAS CALIFORNIA COLORADO CONNECTICUT FLORIDA
##   Midwest     0      0       0        0          0        0           0       0
##   Northeast   0      0       0        0          0        0           1       0
##   South       0      0       0        2          0        0           0       2
##   West        0     10       4        0         32        3           0       0
##            
##             GEORGIA HAWAII IDAHO ILLINOIS INDIANA IOWA KANSAS LOUISIANA
##   Midwest         0      0     0        3       1    1      1         0
##   Northeast       0      0     0        0       0    0      0         0
##   South           1      0     0        0       0    0      0         1
##   West            0      2    20        0       0    0      0         0
##            
##             MICHIGAN MINNESOTA MISSOURI MONTANA NEBRASKA NEVADA NEW HAMPSHIRE
##   Midwest          2         1        1       0        1      0             0
##   Northeast        0         0        0       0        0      0             1
##   South            0         0        0       0        0      0             0
##   West             0         0        0       6        0      4             0
##            
##             NEW JERSEY NEW MEXICO NEW YORK NORTH CAROLINA NORTH DAKOTA OHIO
##   Midwest            0          0        0              0            3    2
##   Northeast          1          0        1              0            0    0
##   South              0          0        0              1            0    0
##   West               0          1        0              0            0    0
##            
##             OKLAHOMA OREGON PENNSYLVANIA SOUTH CAROLINA TENNESSEE TEXAS UNKNOWN
##   Midwest          0      0            0              0         0     0       0
##   Northeast        0      0            1              0         0     0       0
##   South            3      0            0              2         1     5       0
##   West             0     13            0              0         0     0       0
##            
##             UTAH WASHINGTON
##   Midwest      0          0
##   Northeast    0          0
##   South        0          0
##   West         3        198

Export

write.csv(dat, "practice.csv")