英文:
Efficient way to edit index ranges in subsetting calls
问题
Sure, here's the translated text:
问题一直存在,即将代码从索引从零开始的语言移植到索引从一开始的语言,再次...
在这种情况下,我想要移植一个长代码块,类似于
x[0:3] = c(65,43, 22)
x[4:7] = c(23, 17,93, 7)
以此类推,还有几十行。我对正则表达式有一定的了解,因此我可以生成一个模式,搜索例如 "[0:9]{1,}:" 等等,但我不想每次都提取每个数字序列,转换为数字,加一,然后使用 paste0
将它们全部连接起来。是否有更好的方法?另外,是否存在现有的 R
包可以自动"移动"文本字符串中的所有索引?
跟进问题:是的,我可以将此文件转储到源语言中,创建 x
,打印 x
值,然后传递给 R
,但我宁愿不这样做。
英文:
The everpresent problem of porting code from a language whose indices start with zero to a language whose indices start with one, again...
In this case I want to port a long block that goes like
x[0:3] = c(65,43, 22)
x[4:7] = c(23, 17,93, 7)
and so on for dozens of lines. I'm reasonably comfortable with regex, so I can generate a pattern
that searches for , e.g. "\[[0:9]{1,}:" and so on, but I'd hate to have to then extract each numeric sequence, convert to numeric, add one, and then paste0
it all back together.
Is there a better approach, and for that matter, is there an existing R
package that can automagically "shift" all the indices in text strings?
Followup: yeah I could dump this file into the source language, create x
, print x
values, and feed that to R
, but I'd rather not.
答案1
得分: 1
以下是翻译好的内容:
如果您的输入确实如下所示
input <- "x[0..3] = {65, 34, 22}
x[4..7] = {23, 17, 93, 7}"
那么您可以使用 str_replace_all
来帮助进行转换。例如
library(stringr)
output <- str_replace_all(input, pattern=r"((\w+\[)(\d+)\.\.(\d+)\])", replacement=function(x) {
m <- str_match(x, r"((\w+\[)(\d+)\.\.(\d+)\])")
paste0(m[,2], as.numeric(m[,3])+1, ":", as.numeric(m[,4])+1, "]")
})
output <- str_replace_all(output, pattern=r"((\{)([^\}]+)(\}))", replacement="c(\)")
cat(output)
# x[1:4] = c(65, 34, 22)
# x[5:8] = c(23, 17, 93, 7)
这使用一个转换函数来增加匹配中的索引值。
英文:
If your input really looks like this
input <- "x[0..3] = {65, 34, 22}
x[4..7] = {23, 17, 93, 7}"
Then you can use str_replace_all
to help with the transformation. For example
library(stringr)
output <- str_replace_all(input, pattern=r"((\w+\[)(\d+)\.\.(\d+)\])", replacement=function(x) {
m <- str_match(x, r"((\w+\[)(\d+)\.\.(\d+)\])")
paste0(m[,2], as.numeric(m[,3])+1, ":", as.numeric(m[,4])+1, "]")
})
output <- str_replace_all(output, pattern=r"((\{)([^\}]+)(\}))", replacement="c(\)")
cat(output)
# x[1:4] = c(65, 34, 22)
# x[5:8] = c(23, 17, 93, 7)
This uses a transformation function to increment the values of the indexes in the match.
答案2
得分: 0
这样的修改会更容易:
例子:
第一个 <- '[0:3]'
第二个 <- gsub('[[]','[1+(', 第一个)
最后 <- gsub('[]]',')]',第二个)
英文:
Much later, it occurs to me that, instead of trying to replace numerals, modifying the index definitions is much easier.
Example:
frist <- '[0:3]'
secnod <- gsub('[[]','[1+(', frist)
finlaly <- gsub('[]]',')]',secnod)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论