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

你。 提交于 2019-12-02 13:10:22

问题


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

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

回答1:


Try with library("stringi") and:

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



回答2:


Here's a base R approach using mapply with grepl:

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



回答3:


Here's a base R option using Vectorize with grepl:

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


来源:https://stackoverflow.com/questions/34717733/r-data-frame-string-contains-does-column-1-contain-column-2

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