英文:
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 <- c("b_express_xxx", "b_express_yy", "b_express_z")
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 <- c("b_express_1", "b_express_2", "b_express_3")
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] "b_express_1" "b_express_2" "b_express_3"
答案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("(.*_).*(\\d+)", "\\", paste0(var, seq_along(var)))
[1] "b_express_1" "b_express_2" "b_express_3"
答案3
得分: 1
你可以使用paste0
和seq_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 <- c("b_express_xxx", "b_express_yy", "b_express_z")
# new strings
var_solution <- paste0("b_express_", seq_along(var))
print(var_solution)
seq_along(var)
generates a sequence of numbers from 1 to the length of var.paste0("b_express_", seq_along(var))
concatenates "b_express_"
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("[a-z]+$", "", var), seq_along(var))
This will work if your strings do not have the same "b_express" prefix:
var <- c("b_express_c_xxx", "b_anotherprefix_yy", "b_express_z")
paste0(gsub("[a-z]+$", "", var), seq_along(var))
# "b_express_c_1" "b_anotherprefix_2" "b_express_3"
答案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, "[^_]+$", 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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论