r check if string contains special characters

…衆ロ難τιáo~ 提交于 2020-08-27 21:50:44

问题


I am checking if a string contains any special characters. This is what I have, and its not working,

    if(grepl('^\\[:punct:]', val))

So if anybody can tell me what I am missing, that will be helpful.

Special characters

        ~ ` ! @# $ % ^ & * | : ; , ." |

回答1:


As @thelatemail pointed out in the comments you can use:

grepl('[^[:punct:]]', val)

which will result in TRUE or FALSE for each value in your vector. You can add sum() to the beginning of the statement to get the total number of these cases.

You can also use:

grepl('[^[:alnum:]]', val)

which will check for any value that is not a letter or a number.



来源:https://stackoverflow.com/questions/36928870/r-check-if-string-contains-special-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!