简体   繁体   English

用同一行另一列中的值替换数据框的一列中的子字符串

[英]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 : 另外,您可以将submapply一起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.

相关问题 用同一数据框中另一列的对应值替换列中的值 - Replace value in column with corresponding value from another column in same dataframe R 在某些条件下用同一行中另一列的值替换或保留一列中的值 - R Replace value or keep value in one column with value from another column in the same row with certain conditions 在特定条件下,将一列中的值替换为同一行中另一列中的值 - Replace value in one column with value from another column in the same row with certain condition 比较一个 dataframe 中的两对列以检测不匹配并在同一行中显示另一列的值 - Compare two pairs of columns from one dataframe to detect mismatches and show the value from another column in the same row 将单元格的NA值替换为同一数据帧中另一列的值 - Replace the NA value of a cell by the value of another column in the same dataframe 使用另一数据框的一行中的值替换一个数据框的一列中的所有值(按行名和列名匹配) - Replace all values in a column of one dataframe using values in a row of another dataframe (matching by row name and column name) 用另一数据框R的行替换一列中每次出现的因子变量 - Replace every occurrence of factor variable in one column with row from another dataframe R 将数据框中的值替换为同一 dataframe 中另一列的对应值 - Replace value in data frame with corresponding value from another column in same dataframe 基于来自同一 dataframe 的另一列的条形图 - Barplot one column based on another column from the same dataframe R:使用行/列替换另一个数据框的值 - R: Replace values of dataframe from another using row/column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM