Help needed in doing a few operations in the following data set(R)




The above file consists of 4 features namely Hospital_ID, District_ID, Instrument_ID and Projected Revenue. The list of all possible Instrument_IDs are given below:

[1] "Instrument 1"  "Instrument 10" "Instrument 11" "Instrument 13" "Instrument 15"
 [6] "Instrument 18" "Instrument 19" "Instrument 2"  "Instrument 20" "Instrument 3" 
[11] "Instrument 4"  "Instrument 5"  "Instrument 6"  "Instrument 7"  "Instrument 8" 

Say for some given Hospital_ID and District_ID there are just two Instrument_IDs and corresponding Projected Revenue are listed in the file, I want to create rows for the each of the other 14 Instrument_IDs with Projected Revenue = 0. I want to do this for all combinations of Hospital_ID and District_ID in the file.
How do I go about doing it?



I’d do the following:

# Creates a data frame with all possible combinations of IDs
new_df <- expand.grid(
  Hospital_ID = levels(orig_df$Hospital_ID), 
  District_ID = levels(orig_df$District_ID),
  Instrument_ID = levels(orig_df$Instrument_ID)

# Merge the Annual_Projected_Revenue values into the new data frame
new_df <- merge(new_df, orig_df, all.x=TRUE, 
                by=c('Hospital_ID', 'District_ID', 'Instrument_ID'))
new_df$Annual_Projected_Revenue[$Annual_Projected_Revenue)] <- 0


Thank you all for the help!