How to add a 0 amount for source solar in year 1990 to the dataframe below? There's presently no value for solar in 1990.
Data:
year | source | amount |
---|---|---|
1990 | coal | 19203 |
1990 | nuclear | 2345 |
1991 | coal | 18490 |
1991 | nuclear | 2398 |
1991 | solar | 123 |
1992 | ... | ... |
... | ... | ... |
2019 | ... | ... |
Code:
data <- read.csv('annual_generation.csv')
data$source <- as.factor(data$source)
This doesn't work but it's the general idea:
for(i in 1990:2019) {
for (j in data$source) {
if (!data[i][j])
data[i][j] = 0
}
}
Edit: Based on the answer below, this was the final solution:
data <- complete(data, YEAR, STATE, ENERGY.SOURCE,
fill = list(
GEN = 0,
TYPE.OF.PRODUCER = 'Total Electric Power Industry'))
YEAR STATE ENERGY.SOURCE TYPE.OF.PRODUCER GEN
<int><fct> <fct> <fct> <dbl>
1 1990 IL Coal Total Electric Power Industry 54966018
...