如果部分字符串与 R 数据框列中的另一个字符串匹配,如何替换总字符串?

如果部分字符串与 R 数据框列中的另一个字符串匹配,要替换总字符串,我们可以按照以下步骤操作 -

  • 首先,创建一个带有字符串列的数据框。

  • 然后,如果部分字符串与另一个字符串匹配,则使用 gsub 函数替换整个字符串。

示例

创建数据框

让我们创建一个数据框,如下所示 -

String<-sample(c("india1234","china1234","russia3541","uk3541","south
africa3541","nepal1234","sri
lanka1234","canada3541","sudan1234","korea1234","belarus3541"),25,replace=TRUE)
df<-data.frame(String)
df
输出结果

执行时,上述脚本生成以下内容output(this output will vary on your system due to randomization)-

    String
1  south\nafrica3541
2  russia3541
3  sudan1234
4  india1234
5  south\nafrica3541
6  india1234
7  south\nafrica3541
8  korea1234
9  korea1234
10 uk3541
11 india1234
12 korea1234
13 belarus3541
14 belarus3541
15 korea1234
16 china1234
17 sudan1234
18 uk3541
19 india1234
20 korea1234
21 korea1234
22 sudan1234
23 uk3541
24 china1234
25 korea1234

如果部分字符串匹配,则替换总字符串

如果字符串中存在 3541,则使用 gsub 函数将总字符串替换为 No,如下所示 -

String<-sample(c("india1234","china1234","russia3541","uk3541","south
africa3541","nepal1234","sri
lanka1234","canada3541","sudan1234","korea1234","belarus3541"),25,replace=TRUE)
df<-data.frame(String)
df$Replaced_S<-gsub(".*3541.*","No",df$String)
df
输出结果
    String           Replaced_S
1  china1234         china1234
2  korea1234         korea1234
3  russia3541        No
4  sri\nlanka1234    sri\nlanka1234
5  russia3541        No
6  nepal1234         nepal1234
7  korea1234         korea1234
8  russia3541        No
9  sudan1234         sudan1234
10 sudan1234         sudan1234
11 sudan1234         sudan1234
12 south\nafrica3541 No
13 nepal1234         nepal1234
14 nepal1234         nepal1234
15 south\nafrica3541 No
16 nepal1234         nepal1234
17 canada3541        No
18 sri\nlanka1234    sri\nlanka1234
19 china1234         china1234
20 sudan1234         sudan1234
21 south\nafrica3541 No
22 nepal1234         nepal1234
23 china1234         china1234
24 sri\nlanka1234    sri\nlanka1234
25 china1234         china1234

猜你喜欢