I'm trying to do the following: search each patient's list of diagnoses for a 
specific code then create a new column based upon the the presence of the 
specific code.  
Simplified data follows:

con <- textConnection("
ID      DX1     DX2     DX3
1       4109    4280    7102
2       734     311     490
3       4011    42822   4101
")
df <- read.table(con, header = TRUE, strip.white = TRUE, colClasses="character")
#
# I would like to add a column such the result of searching for 410 would give: 
 The search string would always be at the start of a word and doesn't need 
regex.
#
# ID    DX1     DX2     DX3     htn
# 1     4109    4280    7102    1
# 2     734     311     490     0
# 3     4011    42822   4101    1
#
# The following  works but is slow and returns NA if the search string is not 
found:

for (i in 1:nrow(df)) {
    df[i,"htn"] <- any(sapply('410', function(x)  which( grepl(x, df[i, 2:4], 
fixed = TRUE) )))
}

Thanks in advance.  I never fail to learn new things from this list.

--
Who is wise? One who learns from every person.
Who is strong? One who overpowers his evil inclinations.
Who is rich? One who is satisfied with his lot.
Who is honorable? One who honors his fellows.
- Pirkei Avot [excerpt]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to