R 数据框字符串包含:第 1 列是否包含第 2 列?

R data frame string contains: Does column 1 contain column 2?

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

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

我想创建一个逻辑向量来检查 Surname 是否包含在 Email 中。因此结果应该是:

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

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

> 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)

这是使用 Vectorizegrepl 的基础 R 选项:

df1$CheckEmail <- Vectorize(grepl)(df1$Surname, df1$Email)

这是使用 mapplygrepl 的基础 R 方法:

transform(df, CheckEmail = mapply(grepl, Surname, Email))
#  Surname                Email CheckEmail
#1   house  greghouse@gmail.com       TRUE
#2  wilson johnwatson@gmail.com      FALSE