r%in%运算符|控制案例敏感性 [英] r %in% operator | control case sensitivity

查看:85
本文介绍了r%in%运算符|控制案例敏感性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以控制%in%运算符的区分大小写?就我而言,无论输入如何,我都希望它返回true:

Is there a way to control the case sensitivity of the %in% operator? In my case I want it to return true no matter the case of the input:

stringList <- c("hello", "world")
"Hello" %in% stringList
"helLo" %in% stringList
"hello" %in% stringList

将此代码视为可复制的示例,但是在我的实际应用程序中,我还使用左侧的字符串列表,并检查stringList中是否存在单词.

Consider this code as a reproducible example, however in my real application I am also using a list of strings on the left and check for the presence of words from stringList.

推荐答案

使用grepl代替,因为它具有ignore.case参数:

Use grepl instead as it has an ignore.case parameter:

grepl("^HeLLo$",stringList,ignore.case=TRUE)
[1]  TRUE FALSE

第一个参数是正则表达式,因此它具有更大的灵活性,但必须以^开头并以$结尾,以避免拾取子字符串.

The first argument is a regular expression, so it gives you more flexibility, but you have to start with ^ and end with $ to avoid picking up sub-strings.

这篇关于r%in%运算符|控制案例敏感性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆