如何在 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
输出
执行上述脚本后,将生成以下输出(由于随机化,此输出在您的系统上可能会有所不同):
String 1 south
africa3541 2 russia3541 3 sudan1234 4 india1234 5 south
africa3541 6 india1234 7 south
africa3541 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
如果部分字符串匹配,则替换整个字符串
使用 gsub 函数,如果字符串中存在 3541,则将整个字符串替换为 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
lanka1234 sri
lanka1234 5 russia3541 No 6 nepal1234 nepal1234 7 korea1234 korea1234 8 russia3541 No 9 sudan1234 sudan1234 10 sudan1234 sudan1234 11 sudan1234 sudan1234 12 south
africa3541 No 13 nepal1234 nepal1234 14 nepal1234 nepal1234 15 south
africa3541 No 16 nepal1234 nepal1234 17 canada3541 No 18 sri
lanka1234 sri
lanka1234 19 china1234 china1234 20 sudan1234 sudan1234 21 south
africa3541 No 22 nepal1234 nepal1234 23 china1234 china1234 24 sri
lanka1234 sri
lanka1234 25 china1234 china1234
广告