R数据帧字符串包含:列1是否包含列2? [英] R data frame string contains: Does column 1 contain column 2?

查看:204
本文介绍了R数据帧字符串包含:列1是否包含列2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两列的数据框:

I have a dataframe with two columns:

  Surname                Email
1   house  greghouse@gmail.com
2  wilson johnwatson@gmail.com

我想创建一个逻辑向量,检查是否姓氏包含在电子邮件中。因此,结果应该是:

I want to create a logical vector which checks if Surname is contained in Email. The result should therefore be:

  Surname                Email CheckEmail
1   house  greghouse@gmail.com       TRUE
2  wilson johnwatson@gmail.com      FALSE

我尝试过 grep 但似乎 grep 只能在1个或多个实例中查找一个模式。 我特别需要在多个实例中查找多个模式

I tried grep but it seems that grep can only look for one pattern in 1 or more instances. I specifically need to look for multiple patterns in multiple instances.

> grep(df1$Surname,df1$Email)
[1] 1
Warning message:
In grep(df1$Surname, df1$Email) :
  argument 'pattern' has length > 1 and only the first element will be used


推荐答案

library(stringi)和:

df1$CheckEmail <- stri_detect_fixed(df1$Email, df1$Surname)

这篇关于R数据帧字符串包含:列1是否包含列2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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