Skip to content

Commit

Permalink
fix pivot_longer column selection in readRCP()
Browse files Browse the repository at this point in the history
  • Loading branch information
0UmfHxcvx5J7JoaOhFSs5mncnisTJJ6q committed Jul 7, 2022
1 parent 4efd805 commit 2a3bda0
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions R/readRCP.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,31 @@ readRCP <- function(subtype) {

dat <- as.data.frame(do.call(rbind, lapply(list.files(pattern = ".xls$"), read_excel)))

if (subtype=="Waste") {
if (subtype == "Waste") {
dat <- dat %>%
filter(.data$Region %in% c("R5ASIA", "R5LAM", "R5MAF", "R5OECD", "R5REF")) %>%
filter(grepl("Waste", .data$Variable)) %>%
pivot_longer(cols = all_of(setdiff(colnames(.data),
c("Region", "Scenario", "Variable",
"Unit"))),
names_to = 'year') %>%
mutate(year = as.numeric(.data$year)) %>%
separate("Variable", c("type", "source"), sep = " - " ) %>%
rename(region = 'Region', scenario = 'Scenario') %>%
select("region", "year", "scenario", "type","source", "value")

dat$region <- substr(dat$region, 3, 7)
dat$value <- as.numeric(dat$value)

} else if (subtype=="AviationShipping") {
filter(.data$Region %in% c("R5ASIA", "R5LAM", "R5MAF", "R5OECD", "R5REF"),
grepl("Waste", .data$Variable)) %>%
pivot_longer(cols = matches('^[0-9]*$'), names_to = 'year',
names_transform = list(year = as.numeric)) %>%
separate("Variable", c("type", "source"), sep = " - ") %>%
select(region = 'Region', 'year', scenario = 'Scenario', 'type', 'source',
'value') %>%
mutate(region = sub('^R5', '', .data$region))

} else if (subtype == "AviationShipping") {
dat <- dat %>%
filter(.data$Region == "World",
grepl("Aviation|Shipping", .data$Variable)) %>%
pivot_longer(cols = all_of(setdiff(colnames(.data),
c("Region", "Scenario", "Variable",
"Unit"))),
names_to = 'year') %>%
mutate(year = as.numeric(.data$year),
Region = "GLO") %>%
separate("Variable", c("type", "source"), sep = " - " ) %>%
rename(region = 'Region', scenario = 'Scenario') %>%
select("region", "year", "scenario", "type", "source","value")

dat$value <- as.numeric(dat$value)

pivot_longer(cols = matches('^[0-9]*$'), names_to = 'year',
names_transform = list(year = as.numeric)) %>%
mutate(Region = "GLO") %>%
separate("Variable", c("type", "source"), sep = " - ") %>%
select(region = 'Region', 'year', scenario = 'Scenario', 'type', 'source',
'value')
} else {
stop("Invalid subtype. Must be 'Waste' or 'AviationShipping'")
}



dat$scenario <- recode(dat$scenario,
`IMAGE - RCP3-PD (2.6)`="rcp26",
`MiniCAM - RCP 4.5` = "rcp45",
Expand Down

0 comments on commit 2a3bda0

Please sign in to comment.