forked from epinotes/InjuryEpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_diag
18 lines (17 loc) · 937 Bytes
/
create_diag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
create_diag <- function(data, expr, colvec, ignore.case = T){
#regexp=regular expressions for the data
# colvec = vector of the columns of interest (columns with the diagnoses)
require(dplyr, quietly = T)
require(tidyr, quietly = T)
# select the variables of interest
sel <- names(data)[colvec]
# ensure they are all the variables are character vectors and create a data subset of the variables
df <- as_data_frame(data[sel]) %>% mutate_each(funs(as.character))
# a function to assign "1" if the regular expression matched or "0" otherwise
f <- function(x) grepl(expr, x, ignore.case = ignore.case)+0
# apply the function above to all the cells in the data frame "df"
df <- sapply(df, f)
df <- as_data_frame(df) %>% mutate(new_diag = rowSums(., na.rm = TRUE)) %>% select(new_diag)
# the vector of the new variable (new diagnosis) to add to the data frame (data)
as.factor(as.numeric((df[,1] > 0)))
}