Home > Back-end >  Object not found error in R function during importing from excel
Object not found error in R function during importing from excel

Time:01-23

LCI.materials <- read.csv('LCImaterials.csv')

LCA.bridge <- function(length, width, height, thickness, girder.Option, deck.Option, materials) {
 
  prefab.girder.Section <- 0.78
  steel.girders.unitWeight <- 317 #the weight for HEM800 steel profile
  asphalt.Q <- length * width * thickness
  
  materials.split <- split(materials, materials$scope)
  
  # calculate the volume of the deck based on different materials strategies
  if(deck.Option == "RC") {
    deck.volume <- length * width * height
    interventions.deck <- 2.5
  } else if (deck.Option == "PRC") {
    deck.volume <- 0.5 * length * width * height
    interventions.deck <- 2
  } else if (deck.Option == "FRP") {
    deck.volume <- 0.2 * length * width * height
    interventions.deck <- 1
  }
  
  #girder options
  if (girder.Option == "PRC") {
    #get the numbers of girders
    n <-round(width / 3.75, 0)
    interventions.girders <- 2
    #get the volume of the concrete for the prefab girders
    girders.V <- n * prefab.girder.Section * length
  } else if (girder.Option == "steel") {
    n <- round(width / 3, 0)
    girders.V <- n * steel.girders.unitWeight * length
    interventions.girders <- 2
  } else if (girder.Option == "none") {
    n <- 0
    girders.V <- 0
    interventions.girders <- 0
  }
  
asphalt <- mutate(materials.split$asphalt, bridge.Q = asphalt.Q, interventions = 12)
deck <- mutate(materials.split[[deck.Option]], bridge.Q = deck.volume, interventions = interventions.deck)

if (!is.null(materials.split[[girder.Option]])) {
  girders <- mutate(materials.split[[girder.Option]], bridge.Q = girders.V, interventions = interventions.girders)
  LCA.matrix <- rbind(deck, girders, asphalt)
} else {
  LCA.matrix <- rbind(deck, asphalt)
}

LCA.matrix <- mutate(LCA.matrix, TotalMaterials.Q = **strong text**quantities * bridge.Q / 1000,
                     materials.LC = TotalMaterials.Q * interventions,
                     Energy.LC = materials.LC * energy,
                     CO2.LC = materials.LC * CO2 * 1000,
                     NOx.LC = materials.LC * NOx * 1000,
                     SO2.LC = materials.LC * SO2 * 1000)

LCA.results <- list(Energy = sum(LCA.matrix$Energy.LC),
                    CO2 = sum(LCA.matrix$CO2.LC),
                    NOx = sum(LCA.matrix$NOx.LC),
                    SO2 = sum(LCA.matrix$SO2.LC))

return(LCA.results)

}

b.length <- 16 # units: m
b.width <- 15 #units m
bd.depth <- 0.25 #units m
asphalt.tk <- 0.12 #units m

girder.Options <- c("PRC", "steel", "none")
deck.options <- c("RC", "PRC", "FRP")

Option1 <- LCA.bridge(b.length, b.width, bd.depth, asphalt.tk, girder.Options[1], deck.options[1], LCI.materials)

Hello people. This R function is completely working without any problem. It is reading a small table from excel. I am asking this. How can this function understand and read the quantities column well? I made it bold. I am doing a similar thing to this function but I am always getting an error:

Error in LCA.bridge(b.length, b.width, b.height, b.thickness, column.Optionsenter image description here

I want to understand how the first code can recognize/understand/find the "quantities" from small table? How can we read a column from a CSV file in R by using dplyr and mutate? Could you please explain to me? Thanks in advance.

CodePudding user response:

The first line of your code reads the .csv file and transfer it to data.frame object named LCI.materials:

LCI.materials <- read.csv('LCImaterials.csv')

For testing ppurposes I have created such file with 2 lines and loaded it. If we would like to see what's inside, head() function helps:

head(LCI.materials)
  material scope quantities energy   CO2   NOx   SO2
1    Brick brick          1   3.56 0.271 0.050 0.100
2   Cement    RC        167   3.26 0.822 0.177 0.065

As you see, the columns corresponds to these, you have in .csv file, including quantities.

The last parameter of LCA.bridge function is named materials, and when you call this function in the last line of your code:

Option1 <- LCA.bridge(b.length, b.width, bd.depth, asphalt.tk, girder.Options[1], deck.options[1], LCI.materials)

you substitute materials with the LCI.materials data frame. Internally, the functions splits the LCI.materials by the scope:

    materials.split <- split(materials, materials$scope)

There is another observation: your LCA.bridge function looks for asphalt in the materials data frame:

asphalt <- 
      mutate(materials.split$asphalt,
             bridge.Q = asphalt.Q,
             interventions = 12)

However in your example (in small table) there is no such material. Might that be an issue?

Grzegorz

CodePudding user response:

LCI.materials <- read.csv('LCImaterials.csv')

LCA.bridge <- function(length, width, height, thickness, column.Option, abutment.Option, materials) {
    
  steel.unitWeight <- 317 #the weight for HEM800 steel profile
   
  materials.split <- split(materials, materials$scope)
  
  #column options
  if(column.Option == "RC") {
    column.volume <- length * width * height
    interventions.column <- 2
    } else if (column.Option == "steel") {
    column.volume <- length * width * height * thickness * steel.unitWeight
    interventions.column <- 3
   } 
  
  #abutment options
  if (abutment.Option == "RC") {
    abutment.volume <- height * length * 5 # 2 * height * length * 5 / 2 
    interventions.abutment <- 2
    } else if (abutment.Option == "brick") {
    abutment.volume <- height * length * 5 # 2 * height * length * 5 / 2 
    interventions.abutment <- 2
   } 
  
  column <- dplyr::mutate(materials.split[[column.Option]], bridge.Q = column.volume, interventions = interventions.column)
  abutment <- dplyr::mutate(materials.split[[abutment.Option]], bridge.Q = abutment.volume, interventions = interventions.abutment)
  
  LCA.matrix <- rbind(column, abutment)
  
  quantities <- split(materials, materials$quantities)
  
  LCA.matrix <- c(LCA.matrix, TotalMaterials.Q = quantities * bridge.Q / 1000,
                       materials.LC = TotalMaterials.Q * interventions,
                       Energy.LC = materials.LC * energy,
                       CO2.LC = materials.LC * CO2 * 1000,
                       NOx.LC = materials.LC * NOx * 1000,
                       SO2.LC = materials.LC * SO2 * 1000)
  
  LCA.results <- list(Energy = sum(LCA.matrix$Energy.LC),
                      CO2 = sum(LCA.matrix$CO2.LC),
                      NOx = sum(LCA.matrix$NOx.LC),
                      SO2 = sum(LCA.matrix$SO2.LC))
   return(LCA.results)
}

b.length <- 5
b.height <- 5
b.width <- 2
b.thickness <- 0.05

column.Options  <- c("RC","steel")
abutment.Options <- c("brick","RC")

Option1 <- LCA.bridge(b.length, b.width, b.height, b.thickness, column.Options[1], abutment.Options[1], LCI.materials)

Thanks for the answer. it was useful. Actually, this is my code and the previous small table for this code. So there is no problem with asphalt. Thanks to your idea I added this line. and it worked.

quantities <- split(materials, materials$quantities)

but now it can find bridge.Q. Are not these enough to make R find bridge.Q? I wrote bridge.Q = column.volume in these mutates. What should I do @Grzegorz Sapijaszko?

column <- dplyr::mutate(materials.split[[column.Option]], bridge.Q = column.volume, interventions = interventions.column)
      abutment <- dplyr::mutate(materials.split[[abutment.Option]], bridge.Q = abutment.volume, interventions = interventions.abutment)
  •  Tags:  
  • Related