Home > Net >  Dplyr Mutate Syntax
Dplyr Mutate Syntax

Time:01-23

girders <- mutate(materials.split[[girder.Option]], bridge.Q = girders.V, interventions = interventions.girders)

Hello people. I want to ask about mutate function in Dplyr for R. When I see the syntax of mutate, I generally see only two parts in the mutate function. But I have an example above. There are three parts. What does it exactly mean this line? What does the coder want to do there? For example, mutate creates new columns in a table. But what does it mean here girders <- mutate ? is "girders" the new name of the new column which is created? Could you explain this?

CodePudding user response:

The number of parameters in dplyr functions may vary depending upon the context. The idea of the pipeline in dplyr is to pass the result of the previous function as the first (data) parameter to the next function (dplyr verb). So you can substitute the provided line of code with the following dplyr equivalent:

girders <-  materials.split[[girder.Option]] %>%
 mutate(bridge.Q = girders.V, interventions = interventions.girders)

materials.split[[girder.Option]] is set then after %>% passed as the first parameter of mutate. If you add another %>% operator the resulting dataset will be passed to the following verb and so on. In this case the function will not require setting the first dataset parameter.

CodePudding user response:

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)

Thank you very much for the answers and comments. This is my code and I could not make R find bridge.Q. I only have a small table and I want to read values from it and create matrix but I could not. This is my small table:enter image description here

  •  Tags:  
  • Related