[英]Replace substring in one column of dataframe with value from another column in same row
I have dataframe of character columns named new_sgs that looks like this: 我有一个名为new_sgs的字符列的数据框,看起来像这样:
SG.Name RegionCode
1 AW02PASGA001 01
2 AW02PASGA002 01
3 AW02PASGA003 01
4 AW02PASGA004 01
5 AW02PASGA005 01
6 AW02PASGA006 01
...
I want to replace '02' in the strings of column 1 with the string in column2. 我想将column 1的字符串中的'02'替换为column2中的字符串。 This does the job for row 1: 这完成了第1行的工作:
new_sgs$SG.Name[1] <- gsub("AW02", paste0("AW", new_sgs$RegionCode[1]), new_sgs$SG.Name[1])
Is there a way to make this change to every row using one of the apply functions? 有没有一种方法可以使用apply函数之一对每一行进行更改? I've tried 我试过了
sapply(new_sgs, function(x) gsub("AW02", paste0("AW", new_sgs$RegionCode[x]), new_sgs$SG.Name[x]))
but this is what I get: 但这就是我得到的:
SG.Name RegionCode
[1,] NA NA
[2,] NA NA
[3,] NA NA
[4,] NA NA
[5,] NA NA
[6,] NA NA
...
Warning messages:
1: In gsub("AW02", paste0("AW", test$RegionCode[x]), test$SG.Name[x]) :
argument 'replacement' has length > 1 and only the first element will be used
2: In gsub("AW02", paste0("AW", test$RegionCode[x]), test$SG.Name[x]) :
argument 'replacement' has length > 1 and only the first element will be used
Thanks! 谢谢!
Luke 路加
If it is guaranteed that the string you want to replace comes at position of 3 and 4 of the Name, you can just use substr
: 如果可以确保要替换的字符串位于Name的3和4位置,则可以使用substr
:
substr(df$SG.Name, 3, 4) <- df$RegionCode
df
# SG.Name RegionCode
#1 AW01PASGA001 01
#2 AW01PASGA002 01
#3 AW01PASGA003 01
#4 AW01PASGA004 01
#5 AW01PASGA005 01
#6 AW01PASGA006 01
Alternatively you can use sub
with mapply
: 另外,您可以将sub
与mapply
一起mapply
:
df$SG.Name = mapply(function(rc, nam) sub("\\d+", nam, rc), df$RegionCode, df$SG.Name, USE.NAMES = F)
str_replace()
from the stringr package will vectorise over a pattern and replacement as you need. 字符串包中的str_replace()
将根据需要对模式进行矢量化和替换。 See example below: 请参见下面的示例:
library(stringr)
x <- data.frame(
SG.Name = c("AW02PASGA001", "AW02PASGA002", "AW02PASGA003"),
RegionCode = c("01", "01", "01")
)
str_replace(x$SG.Name, "02", x$RegionCode)
#> [1] "AW01PASGA001" "AW01PASGA002" "AW01PASGA003"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.