gma*_*ais 1 string r contains dataframe
我有一个包含两列的数据框:
Surname Email
1 house greghouse@gmail.com
2 wilson johnwatson@gmail.com
Run Code Online (Sandbox Code Playgroud)
我想创建一个逻辑向量来检查是否Surname包含在中Email.结果应该是:
Surname Email CheckEmail
1 house greghouse@gmail.com TRUE
2 wilson johnwatson@gmail.com FALSE
Run Code Online (Sandbox Code Playgroud)
我试过grep但似乎grep只能在一个或多个实例中查找一个模式.我特别需要在多个实例中查找多个模式.
> 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
Run Code Online (Sandbox Code Playgroud)
尝试使用library("stringi")和:
df1$CheckEmail <- stri_detect_fixed(df1$Email, df1$Surname)
Run Code Online (Sandbox Code Playgroud)
下面是一个使用基础R的方法mapply有grepl:
transform(df, CheckEmail = mapply(grepl, Surname, Email))
# Surname Email CheckEmail
#1 house greghouse@gmail.com TRUE
#2 wilson johnwatson@gmail.com FALSE
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
401 次 |
| 最近记录: |