簡體   English   中英

在 R 中動態重命名矩陣的有效方法

[英]Efficient way to dynamically rename a matrix in R

我目前正在嘗試向矩陣動態添加值。 代碼結構如下:

for(f in length(file)) # loop through all files, which is 42 files in total
for(angle in c(0,45,90,135){
    for(dis in c(1,3,5){
         textureFeature2[f,(21*count - 20):(21*count)] <- as.matrix(calc_features(hbGLCM))
    }
}

矩陣將在 12 次循環后填充。 在每個循環中,一旦將新值分配給“textureFeature2”矩陣,我還想重命名新列。 calc_features 返回的值是 dataframe 並且具有 colnames,但是一旦轉換為矩陣並分配給 textureFeature2 矩陣,此信息就會丟失。 我這樣做的方法很愚蠢:

names(textureFeature2)[names(textureFeature2)      # Rename two variable names
                           %in% c(paste('V',count,sep = ''),
                                  paste('V',count+1,sep = ''),
                                  paste('V',count+2,sep = ''),
                                  paste('V',count+3,sep = ''),
                                  paste('V',count+4,sep = ''),
                                  paste('V',count+5,sep = ''),
                                  paste('V',count+6,sep = ''),
                                  paste('V',count+7,sep = ''),
                                  paste('V',count+8,sep = ''),
                                  paste('V',count+9,sep = ''),
                                  paste('V',count+10,sep = ''),
                                  paste('V',count+11,sep = ''),
                                  paste('V',count+12,sep = ''),
                                  paste('V',count+13,sep = ''),
                                  paste('V',count+14,sep = ''),
                                  paste('V',count+15,sep = ''),
                                  paste('V',count+16,sep = ''),
                                  paste('V',count+17,sep = ''),
                                  paste('V',count+18,sep = ''),
                                  paste('V',count+19,sep = ''),
                                  paste('V',count+20,sep = '')
                                  )] <-
          c(paste(featureAffix, '_mean_',angle,'_',d,sep = ''),
            paste(featureAffix, '_variance_',angle,'_',d,sep = ''),
            paste(featureAffix, '_autoCorrelation_',angle,'_',d,sep = ''),
            paste(featureAffix, '_cProminence_',angle,'_',d,sep = ''),
            paste(featureAffix, '_cShade_',angle,'_',d,sep = ''),
            paste(featureAffix, '_cTendency_',angle,'_',d,sep = ''),
            paste(featureAffix, '_contrast_',angle,'_',d,sep = ''),
            paste(featureAffix, '_correlation_',angle,'_',d,sep = ''),
            paste(featureAffix, '_differentEntropy_',angle,'_',d,sep = ''),
            paste(featureAffix, '_dissimilarity_',angle,'_',d,sep = ''),
            paste(featureAffix, '_energy_',angle,'_',d,sep = ''),
            paste(featureAffix, '_entroypy_',angle,'_',d,sep = ''),
            paste(featureAffix, '_homogeneity1_',angle,'_',d,sep = ''),
            paste(featureAffix, '_homogeneity2_',angle,'_',d,sep = ''),
            paste(featureAffix, '_IDMN_',angle,'_',d,sep = ''),
            paste(featureAffix, '_IDN_',angle,'_',d,sep = ''),
            paste(featureAffix, '_inverseVariance_',angle,'_',d,sep = ''),
            paste(featureAffix, '_maxProb_',angle,'_',d,sep = ''),
            paste(featureAffix, '_sumAverage_',angle,'_',d,sep = ''),
            paste(featureAffix, '_sumEntropy_',angle,'_',d,sep = ''),
            paste(featureAffix, '_sumVariance_',angle,'_',d,sep = '')
          )
      }
    }

但這不起作用,因為新列沒有重命名。 有什么聰明的方法可以做到這一點嗎?

由於paste是矢量化的,我們可以將多個paste調用更改為單個調用

paste('V', count + 0:20)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM