【发布时间】:2018-09-17 14:50:49
【问题描述】:
我目前正在处理具有多个相似列的数据集:item1、item2 等到 item8。在我正在编写的函数中,我创建了一个特定的搜索,用作 ifelse 函数中的第一个参数,该函数在以下任何列中搜索短语:
grepl(exact_name, item1) | grepl(exact_name, item2) | grepl(exact_name, item3) | grepl(exact_name, item4) | grepl(exact_name, item5) | grepl(exact_name, item6) | grepl(exact_name, item7) | grepl(exact_name, item8)
这很好用,但我希望能够将此函数应用于具有未知数量的项目列的数据框(所有使用的数据框将至少有一个项目列,如果它只有一个,它将仍然是 item1 的格式,而不仅仅是 item)。我尝试使用 * 通配符如下:
grepl(exact_name, item*)
但我在 item* 后面加上括号时出现错误,甚至无法运行该部分。有没有更好的方法来做到这一点?
【问题讨论】: