0

I need to make a matrix by extracting certain information from a file called flowdata. I need to extract the meanValue of all inputGroup == 5. As the column names, I need the corresponding information on the "name" column, and as row names I need the information written on the cell 4 above the "inputGroup == 5" [i-4]. In this case "Methanol, at plant"

Here is the link to the dropbox that has the data https://www.dropbox.com/s/x2knuqq1odbt5zg/flowdata01.txt?dl=0

Here is the R code I have:

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

`

When I run the above R code, I get this error message:

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

This is how the matrix should look like

0 Answers0