我的数据看起来像这样:
sources
1: Jana’s iPhone
2: Richard's iPhone 6
3: Denise's
4: Sara’s iPhone
5: Jeff’s Apple Watch
6: BLAIR’s Apple Watch
7: Sunshine's iPhone
8: Brian's iPhone
9: Jonathan’s Apple Watch
10: patricia’s Apple Watch
Run Code Online (Sandbox Code Playgroud)
我正在尝试替换任何包含iPhone说的字符串iPhone.我怎样才能做到这一点?
我在sources包含一个名为的列的数据表上尝试了以下内容sources:
sources[length(grep("iPhone", sources)) > 0, sources:= "iPhone"]
Run Code Online (Sandbox Code Playgroud)
但是这会将所有行转换为"iPhone",即使该行最初不包含带有"iPhone"的字符串.我猜这是因为grep或length没有矢量化,所以我最终选择了所有行.那么我的问题就是如何识别包含子字符串的行?
grepl改为使用:
sources[grepl("iPhone", sources), sources:= "iPhone"]
Run Code Online (Sandbox Code Playgroud)