用R中字符串的索引替换字符串的一部分

huangapple go评论62阅读模式
英文:

Replacing part of strings with the index of the string in R

问题

var <- c("b_express_xxx", "b_express_yy", "b_express_z")

我想要更新在“express_”之后的字符串,使其成为向量中字符串的索引。这是期望的输出:

var_solution <- c("b_express_1", "b_express_2", "b_express_3")

我该如何做呢?我知道可以使用gsub,但我不确定如何用不同的索引来替换它。谢谢!

英文:

I have a vector that looks like this:

var &lt;- c(&quot;b_express_xxx&quot;, &quot;b_express_yy&quot;, &quot;b_express_z&quot;)

I want to update the string after "express_" to be the index of the string in the vector. This is the desired output:

var_solution &lt;- c(&quot;b_express_1&quot;, &quot;b_express_2&quot;, &quot;b_express_3&quot;)

How can I do this? I know I can use gsub, but I'm not sure how to replace it with different indices. Thank you!

答案1

得分: 1

如果它们都有相同的前缀,使用 substr 的快速方法只需将前缀与索引一起粘贴(使用 seq_along()):

paste0(substr(var, 1, 10), seq_along(var))

[1] "b_express_1" "b_express_2" "b_express_3"
英文:

If they all have the same prefix, a quick way with substr would be to simply paste the prefix alongside the index (using seq_along()):

paste0(substr(var, 1, 10), seq_along(var))

[1] &quot;b_express_1&quot; &quot;b_express_2&quot; &quot;b_express_3&quot;

答案2

得分: 1

使用 `sub` 和替换组的方法

sub("(._).(\d+)", "\1\2", paste0(var, seq_along(var)))
[1] "b_express_1" "b_express_2" "b_express_3"

英文:

An approach using sub and replacement groups

sub(&quot;(.*_).*(\\d+)&quot;, &quot;\\&quot;, paste0(var, seq_along(var)))
[1] &quot;b_express_1&quot; &quot;b_express_2&quot; &quot;b_express_3&quot;

答案3

得分: 1

你可以使用paste0seq_along来根据向量中字符串的索引生成新的字符串。下面是如何做到这一点的示例:

# 原始向量
var <- c("b_express_xxx", "b_express_yy", "b_express_z")

# 新字符串
var_solution <- paste0("b_express_", seq_along(var))

print(var_solution)

seq_along(var)生成从1到var的长度的数字序列。paste0("b_express_", seq_along(var))"b_express_"与序列中的每个数字连接起来。

英文:

You can get this by using paste0 and seq_along to generate a new string based on the index of the string in the vector. Here's how you can do this,

# original vector
var &lt;- c(&quot;b_express_xxx&quot;, &quot;b_express_yy&quot;, &quot;b_express_z&quot;)

# new strings
var_solution &lt;- paste0(&quot;b_express_&quot;, seq_along(var))

print(var_solution)

seq_along(var) generates a sequence of numbers from 1 to the length of var.paste0(&quot;b_express_&quot;, seq_along(var)) concatenates &quot;b_express_&quot; with each number in the sequence.

答案4

得分: 1

paste0(gsub("[a-z]+$", "", var), seq_along(var))
英文:

Remove all lowercase letters before the end of the string and then paste on the values.

paste0(gsub(&quot;[a-z]+$&quot;, &quot;&quot;, var), seq_along(var))

This will work if your strings do not have the same "b_express" prefix:

var &lt;- c(&quot;b_express_c_xxx&quot;, &quot;b_anotherprefix_yy&quot;, &quot;b_express_z&quot;)

paste0(gsub(&quot;[a-z]+$&quot;, &quot;&quot;, var), seq_along(var))
# &quot;b_express_c_1&quot;     &quot;b_anotherprefix_2&quot; &quot;b_express_3&quot;

答案5

得分: 0

stringr::str_replace_all(var, "[^_]+$", as.character(seq_along(var)))

[1] "b_express_1" "b_express_2" "b_express_3"

`regmatches<-`(var, regexpr("[^_]+$", var), value = seq_along(var))
[1] "b_express_1" "b_express_2" "b_express_3"

If you do not mind doing inplace replacement:

regmatches(var, regexpr("[^_]+$", var)) <- seq_along(var)
var
[1] "b_express_1" "b_express_2" "b_express_3"
英文:
stringr::str_replace_all(var, &quot;[^_]+$&quot;, as.character(seq_along(var)))

[1] &quot;b_express_1&quot; &quot;b_express_2&quot; &quot;b_express_3&quot;

`regmatches&lt;-`(var, regexpr(&quot;[^_]+$&quot;, var), value = seq_along(var))
[1] &quot;b_express_1&quot; &quot;b_express_2&quot; &quot;b_express_3&quot;

If you do not mind doing inplace replacement:

regmatches(var, regexpr(&quot;[^_]+$&quot;, var)) &lt;- seq_along(var)
var
[1] &quot;b_express_1&quot; &quot;b_express_2&quot; &quot;b_express_3&quot;

huangapple
  • 本文由 发表于 2023年7月28日 03:59:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定