I tried to use a 'function' in r for efficiency, but it seems that I get different results or no result.
When run directly, the result is,
> data1$CI_allergy <- str_extract(data1$CUR_ILL, "allergy") 
> data1$CI_allergy <- ifelse(data1$CI_allergy == "allergy", 1, 0) 
> data1$CI_allergy[is.na(data1$CI_allergy)] <-0 data1$CI_allergy <-
> ifelse(data1$CI_allergy == 0, "N", "Y") 
> 
> table(data1$CI_allergy)
      N       Y 
2714383   21642 
However, when the function is used:
CI_variable <- function(arg1, arg2) {
  data1$arg1 <- str_extract(data1$CUR_ILL, 'arg2') 
  data1$arg1 <- ifelse(data1$arg1 == 'arg2', 1, 0) 
  data1$arg1[is.na(data1$arg1)] <-0
  data1$arg1 <- ifelse(data1$arg1 == 0, "N", "Y") 
  return(table(data1$arg1))
}
CI_variable(CI_allergy, allergy)
    N 
2736025 
I am guessing the error occurred in str_extract function in CI_variable, but not sure.
Has anyone had a similar problem and solved it?
 
     
    