-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathErrorCode.R
More file actions
182 lines (158 loc) · 6.2 KB
/
Copy pathErrorCode.R
File metadata and controls
182 lines (158 loc) · 6.2 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
## ----setup, include=FALSE-------------------------------------------------------------------------------------------------------------------------
#Installs packages if not previously installed, press run twice if first time installing packages
if(!require(haven)) {install.packages("haven", repos = "http://cran.us.r-project.org"); library(haven)}
if(!require(readr)) {install.packages("readr", repos = "http://cran.us.r-project.org"); library(readr)}
if(!require(dplyr)) {install.packages("dplyr", repos = "http://cran.us.r-project.org"); library(dplyr)}
## -------------------------------------------------------------------------------------------------------------------------------------------------
# Replace with your filename
filename <- ""
sav <- read_sav(paste0("./data/", filename, ".sav"))
write_csv(x = sav, file = paste0("./", filename, ".csv"))
data <- read_csv(paste0("./data/", filename, ".csv"), show_col_types = F)
merged_data <- data %>%
group_by(record_id) %>%
summarize_all(list(~ first(.[!is.na(.)])))
## -------------------------------------------------------------------------------------------------------------------------------------------------
#Used to find the variables in this data set which contain the word date.
date_variables <- merged_data %>%
select(contains("date"))
#Used to find the indices in this data set for the variables which contain the word date.
date_indices <- grep("date", names(merged_data))
#View date variables and their indices
#date_variables
#date_indices
#for session_date_virtual
for (i in seq(date_indices[1] + 1, date_indices[2] - 1)) {
# Check if the column is numeric or character
is_numeric <- is.numeric(merged_data[[i]])
is_character <- is.character(merged_data[[i]])
# Flag to check if a date is encountered for each row
date_encountered <- rep(FALSE, nrow(merged_data))
# Iterate through rows
for (j in 1:nrow(merged_data)) {
# If a date is encountered in the first date column
if (!is.na(merged_data[j, date_indices[1]])) {
date_encountered[j] <- TRUE
}
# If a date has been encountered, update NA values after the first date column
if (date_encountered[j]) {
if (is.na(merged_data[j, i])) {
# Fill in -777 if date has been encountered, -888 otherwise
if (is_numeric) {
merged_data[j, i] <- -777
} else if (is_character) {
merged_data[j, i] <- "-777"
}
}
} else if (is.na(merged_data[j, i])) {
# Fill in -888 if date has not been encountered
if (is_numeric) {
merged_data[j, i] <- -888
} else if (is_character) {
merged_data[j, i] <- "-888"
}
}
}
}
#Do the same for eeg_date
for (i in seq(date_indices[2] + 1, date_indices[3] - 1)) {
# Check if the column is numeric or character
is_numeric <- is.numeric(merged_data[[i]])
is_character <- is.character(merged_data[[i]])
# Flag to check if a date is encountered for each row
date_encountered <- rep(FALSE, nrow(merged_data))
# Iterate through rows
for (j in 1:nrow(merged_data)) {
# If a date is encountered in the first date column
if (!is.na(merged_data[j, date_indices[1]])) {
date_encountered[j] <- TRUE
}
# If a date has been encountered, update NA values after the first date column
if (date_encountered[j]) {
if (is.na(merged_data[j, i])) {
# Fill in -777 if date has been encountered, -888 otherwise
if (is_numeric) {
merged_data[j, i] <- -777
} else if (is_character) {
merged_data[j, i] <- "-777"
}
}
} else if (is.na(merged_data[j, i])) {
# Fill in -888 if date has not been encountered
if (is_numeric) {
merged_data[j, i] <- -888
} else if (is_character) {
merged_data[j, i] <- "-888"
}
}
}
}
#Do the same for smk_date
for (i in seq(date_indices[3] + 1, date_indices[4] - 1)) {
# Check if the column is numeric or character
is_numeric <- is.numeric(merged_data[[i]])
is_character <- is.character(merged_data[[i]])
# Flag to check if a date is encountered for each row
date_encountered <- rep(FALSE, nrow(merged_data))
# Iterate through rows
for (j in 1:nrow(merged_data)) {
# If a date is encountered in the first date column
if (!is.na(merged_data[j, date_indices[1]])) {
date_encountered[j] <- TRUE
}
# If a date has been encountered, update NA values after the first date column
if (date_encountered[j]) {
if (is.na(merged_data[j, i])) {
# Fill in -777 if date has been encountered, -888 otherwise
if (is_numeric) {
merged_data[j, i] <- -777
} else if (is_character) {
merged_data[j, i] <- "-777"
}
}
} else if (is.na(merged_data[j, i])) {
# Fill in -888 if date has not been encountered
if (is_numeric) {
merged_data[j, i] <- -888
} else if (is_character) {
merged_data[j, i] <- "-888"
}
}
}
}
#Do the same for session_date
for (i in seq(date_indices[4] + 1, ncol(merged_data))) {
# Check if the column is numeric or character
is_numeric <- is.numeric(merged_data[[i]])
is_character <- is.character(merged_data[[i]])
# Flag to check if a date is encountered for each row
date_encountered <- rep(FALSE, nrow(merged_data))
# Iterate through rows
for (j in 1:nrow(merged_data)) {
# If a date is encountered in the first date column
if (!is.na(merged_data[j, date_indices[1]])) {
date_encountered[j] <- TRUE
}
# If a date has been encountered, update NA values after the first date column
if (date_encountered[j]) {
if (is.na(merged_data[j, i])) {
# Fill in -777 if date has been encountered, -888 otherwise
if (is_numeric) {
merged_data[j, i] <- -777
} else if (is_character) {
merged_data[j, i] <- "-777"
}
}
} else if (is.na(merged_data[j, i])) {
# Fill in -888 if date has not been encountered
if (is_numeric) {
merged_data[j, i] <- -888
} else if (is_character) {
merged_data[j, i] <- "-888"
}
}
}
}
# View the updated merged_data
#View(merged_data)
write_sav(data = merged_data, path = paste0("./", filename, "_errorcode.sav"))