R

时间:2017-07-17 02:23:24

标签: r loops matrix data-extraction

我需要通过从名为 flowdata 的文件中提取某些信息来制作矩阵。我需要提取所有 inputGroup == 5 meanValue 。作为列名,我需要关于" name"的相应信息。列,作为行名称,我需要在" inputGroup == 5"上面的单元格4上写入信息。 [I-4]。在这种情况下"甲醇,在工厂"

以下是包含数据的保管箱的链接 https://www.dropbox.com/s/x2knuqq1odbt5zg/flowdata01.txt?dl=0

这是我的R代码:

flowdata <-flowdata01

    in.up = length(unique(flowdata$name)) # number of unit processes
    in.p = length(unique(flowdata$.attrs[which(flowdata$metadata=='name')])) # number of inputs/outputs

    input.mat = matrix(0, in.p,in.up) #empty matrix

    colnames(input.mat) = unique(flowdata$name) # up names
    rownames(input.mat) = unique(flowdata$attrs[which(flowdata$metadata=='name')]) # inputs/outputs names

    for (i in 1:nrow(flowdata)){ # for every row in flowdata

      if (flowdata$metadata[i]=="inputGroup" && flowdata$attrs[i] == 5){ # if it is an inputGroup 5 

        col.name = flowdata$name[i] # up name
        row.name = flowdata$attrs[i-4] # i/o name 4 cells above

        value = as.numeric(flowdata$attrs[i-5]) #value 5 cells above

        input.mat[row.name,col.name] = value}}

input.mat = input.mat[-which(rowSums(input.mat)==0),] # if the row is empty, then the flow was an input or output of no interest

`

当我运行上面的R代码时,我收到以下错误消息:

  Error in `[<-`(`*tmp*`, row.name, col.name, value = 6397) : 
  subscript out of bounds

This is how the matrix should look like

0 个答案:

没有答案