简体   繁体   English

基于 R 中的模式列表的模糊匹配

[英]fuzzy match based on a list of patterns in R

I need to generate a dummy variable based on a list of patterns.我需要根据模式列表生成一个虚拟变量。

df <- data.frame(
  med = c("sivastatin", "sisvatatin", "rusvastatin", "yes", "no", "don't remember", "true", "false", "omega 3", "atorvastatin", "no")
)

I need to create a second dummy variable that indicates if the patient used or not any med.我需要创建第二个虚拟变量来指示患者是否使用了任何药物。 I tried this:我试过这个:

yes <- c("yes", "vastatin", "true", "don't remember")

nao <- c("no", "false") 

df$med_cat <- ifelse(agrepl(yes, df$med, ignore.case = TRUE), 1, 
                  ifelse(agrepl(no, df$med, ignore.case = TRUE), 0, NA)) 

But I'm getting an error saying that only the first element is being used但是我收到一个错误,说只使用了第一个元素

argument 'pattern' length > 1 and only the first element is going to used Error in $<-.data.frame ( *tmp* , med_cat, value = logical(0)): replacement has 0 rows, data has 8381参数“模式”长度 > 1,并且只有第一个元素将使用$<-.data.frame中的错误( *tmp* ,med_cat,值 = 逻辑(0)):替换有 0 行,数据有 8381

can someone help me with this?有人可以帮我弄这个吗?

SOLUTION:解决方案:

df$med_cat <- ifelse(apply(sapply(yes, agrepl, df$med_cholstand, .1), 1, any), 1,
                             ifelse(apply(sapply(no, agrepl, df$med_cholstand, .1), 1, any), 0, NA))

Thank you all for your help!谢谢大家的帮助!

This is the solution to the problem:这是问题的解决方案:


df$med_cat <- ifelse(apply(sapply(yes, agrepl, df$med_cholstand, .1), 1, any), 1,
                             ifelse(apply(sapply(no, agrepl, df$med_cholstand, .1), 1, any), 0, NA))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM