I have an R data processing problem. I have a large set of data in a .csv file that I can load in using readr
. The problem is that there are missing values that I need to add to the dataframe.
Example data:
X1 X2 Value
Coal 1 300
Coal 3 100
Coal 5 150
NatG 2 175
NatG 4 150
This file will load in correctly. However, I want to add processing so that the final dataframe contains an entry for every X1 type and the entire sequence 1:5 in X2 with the missing values set equal to zero. The final dataframe would like like this:
X1 X2 Value
Coal 1 300
Coal 2 0
Coal 3 100
Coal 4 0
Coal 5 150
NatG 1 0
NatG 2 175
NatG 3 0
NatG 4 150
NatG 5 0
I tried using readr to load in a file that had all entries equal to zero and then read in the real data to the same data frame, but it was a destructive overwrite and the missing rows were removed. I need to know how to add appropriate rows to the dataframe without knowing which values in the 1:5 sequence will be missing for each value under X1.