简体   繁体   中英

R: Add 0s to dataframe

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
  ...

We can use complete from tidyr

library(tidyr)
complete(data, year, source, fill = list(amount = 0))

-output

# A tibble: 6 x 3
#   year source  amount
#  <int> <chr>    <dbl>
#1  1990 coal     19203
#2  1990 nuclear   2345
#3  1990 solar        0
#4  1991 coal     18490
#5  1991 nuclear   2398
#6  1991 solar      123

Also, if there are some 'year', missing. we can use a range

complete(data, year = 1990:2019, source, fill = list(amount = 0))

data

data <- structure(list(year = c(1990L, 1990L, 1991L, 1991L, 1991L), 
source = c("coal", 
"nuclear", "coal", "nuclear", "solar"), amount = c(19203L, 2345L, 
18490L, 2398L, 123L)), class = "data.frame", row.names = c(NA, 
-5L))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM