Опция с dcast
library(data.table)
dcast(setDT(costs), scenario + component ~ site,
value.var = 'cost_amt')[, value := Inpatient -NASON][]
# scenario component Inpatient NASON value
#1: Base Drugs 12561 12561 0
#2: Base Hospitalization 53360 23526 29834
#3: Base Procedures 15690 12537 3153
#4: Base Total 81611 48624 32987
#5: Scenario Drugs 6460 6460 0
#6: Scenario Hospitalization 42003 16304 25699
#7: Scenario Procedures 10313 8718 1595
#8: Scenario Total 58776 31482 27294
data
costs <- structure(list(site = c("Inpatient", "Inpatient", "Inpatient",
"Inpatient", "NASON", "NASON", "NASON", "NASON", "Inpatient",
"Inpatient", "Inpatient", "Inpatient", "NASON", "NASON", "NASON",
"NASON"), component = c("Total", "Hospitalization", "Procedures",
"Drugs", "Total", "Hospitalization", "Procedures", "Drugs", "Total",
"Hospitalization", "Procedures", "Drugs", "Total", "Hospitalization",
"Procedures", "Drugs"), cost_amt = c(81611L, 53360L, 15690L,
12561L, 48624L, 23526L, 12537L, 12561L, 58776L, 42003L, 10313L,
6460L, 31482L, 16304L, 8718L, 6460L), scenario = c("Base", "Base",
"Base", "Base", "Base", "Base", "Base", "Base", "Scenario", "Scenario",
"Scenario", "Scenario", "Scenario", "Scenario", "Scenario", "Scenario"
)), class = "data.frame", row.names = c("1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"))