forked from vynguyen92/nhanes_mortality_associations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
f - clean_questionnaire_dataset.R
57 lines (49 loc) · 2.14 KB
/
f - clean_questionnaire_dataset.R
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
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
############################## FUNCTION TO CLEAN THE NHANES QUESTIONNAIRE DATASET ############################
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# Purpose: This function cleans the questionnaire dataset to contain data on comorbidity and number of comorbidities
# for each participant
#
# Inputs: dataset_unclean - dataframe of unclean questionnaire data
#
# Outputs: dataset_clean - dataframe of the cleaned questionnaire dataset containing only the selected comorbidities
clean_questionnaire_dataset <- function(dataset_unclean)
{
# Select the columns pertaining to the pertinent conditions
dataset_clean <- dataset_unclean %>%
select(SEQN
, DIQ010
, MCQ010
, MCQ160A
, MCQ160B
, MCQ160E
, MCQ160F
, MCQ220
, MCQ160L
, MCQ160G
, MCQ160K
, study_year)
# 3 indicates that the condition is present in the participant
dataset_clean[dataset_clean == 3] <- 1
# 2 indicates that the condition is not present in the participant
dataset_clean[dataset_clean == 2] <- 0
# 7 and 9 indicates that the participant refused to disclose the presence or absence of the condition and
# do not of having such a condition, respectively
dataset_clean[dataset_clean == 7] <- NA
dataset_clean[dataset_clean == 9] <- NA
# Calculate the number of conditions present for each participant
dataset_clean <- dataset_clean %>%
mutate(VNMORBIDITY = select(.
, DIQ010
, MCQ010
, MCQ160A
, MCQ160B
, MCQ160E
, MCQ160F
, MCQ220
, MCQ160L
, MCQ160G
, MCQ160K) %>%
rowSums(na.rm = TRUE))
return(dataset_clean)
}