在 R 中,是否可以对矩阵行和列索引使用动态公式?

In R, is it possible to use a dynamic formula for matrix row and column indices?

下面的代码部分可以很好地从垂直扩展矩阵中提取值,一次向下添加一行(请特别注意我在 # << 代码中的注释):

plotData <- reactive({
    req(dim(sanitizedMat())[1] >= 1)
    lapply(seq_len(nrow(sanitizedMat())),
           function(i){
             tibble(
               Scenario = rownames(sanitizedMat())[i],
               X = 1:input$periods,
               Y = interpol(input$periods, sanitizedMat()[i, 1:2]) # << note the matrix index [i,1:2]
             )
           }) %>% bind_rows()
  })

但是,在我尝试将 2 的数据对中的矩阵读数重新定向到水平方向时,我想更改矩阵索引,如下所示,其中 interpol() 函数应用于 Y (强调注释 # << ):

plotData <- reactive({
    req(dim(sanitizedMat())[1] >= 1)
    lapply(seq_len(ncol(sanitizedMat()))/2,
           function(i){
             tibble(
               Scenario = rownames(sanitizedMat())[i],
               X = 1:input$periods,
               Y = interpol(input$periods, sanitizedMat()[1, i*2-1:i*2]) # << note attempt to use formula for matrix index
             )
           }) %>% bind_cols()
  })

是否可以使用矩阵索引的公式,就像我在上面尝试使用 [1, i*2-1:i*2] 一样?如果可能的话,我很确定我的语法是错误的,但这说明了这个想法。

这将允许我水平跳过矩阵,在 2 列的跳跃中作为 lapply 通过 i 迭代。

参考 post ,它反映了动态矩阵索引并提供了完全解析的代码。