merge_prov: Merges provinces

Description Usage Arguments Value Examples

View source: R/merge_prov.R

Description

Tidy the data and merges data accordingly to a time range and by the provinces concerned by a split/combined event and return a data frame for the time range imputed.

Usage

1
2
merge_prov(df, sel = names(df), FUN = sum, from, to = "2017-12-31",
  diseases = NULL, df2 = NULL, args = NULL, FUN2 = sum, ...)

Arguments

df

A data frame containing at least the variables province, year, accept also the monthly data if the month are in the column month.

sel

A vector of character to select only the variable to merge. By default, select all the variables.

FUN

A function to apply on the data when merging the province together. By default, sum.

from

Initial date of the time range selected for the province definition, of the class Date or numeric.

to

Final date of the time range selected for the province definition, of the class Date or numeric, by default "2017-12-31"

diseases

A vector of character used to know which history of Vietnam has to be taken for the merging event as two diseases has different story. Used if you want the same merging event as your infectious disease dataframe, for more details look at the gdpm package. By default, NULL

df2

A data frame containing at least the variables province, year. Can be used to provide additional arguments through the paramaters args by providing the name of the column(s) containing the data.

args

string vector, column name of the additional arguments

FUN2

A function to apply on df2 when merging the province together, to keep the same information at every step of the merging process. By default sum

...

additional arguments to pass to FUN and FUN2.

Value

A object of the same class as df in which all the provinces that needed to be merged (according to the time range) are merged.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# For all the following examples, we will use the data from the package gso.
library(gso) # for the "content"
library(magrittr) # for "%>%"
library(dplyr)   # for "filter"
migration_rate <- gso::content %>% filter(data_name == "demography_12") %>%
  .$data %>% .[[1]] %>%
  mutate(year = as.numeric(year))

# if you want to have the data expressed by province, with the province's
# definition of 1992 in Vietnam:
merge_prov(migration_rate, from = "1992-01-01")
# If you want the province's definition between 1992 and 2010 in Vietnam:
merge_prov(migration_rate, from = 1992, to = 2010)

# You can change the function
merge_prov(migration_rate, from = "1992", FUN = mean)

# You can also use weighted mean by providing the weighted in another
# data frame
pop_size <-  gso::content %>% filter(data_name == "demography_5") %>%
  .$data %>% .[[1]] %>% dplyr::select(province, year, total) %>%
  mutate(year = as.numeric(year))
merge_prov(migration_rate, from = "1992-01-01", FUN = weighted.mean,
  df2 = pop_size, args = "total")

# You can define the merge_prov function only on certain columns
pop_info <- gso::content %>% filter(data_name == "demography_1") %>%
  .$data %>% .[[1]] %>%
  mutate(year = as.numeric(year))
merge_prov(pop_info, sel = "average_population_thous_pers",
  from = 1992, FUN = weighted.mean,
  df2 = pop_size, args = "total")

choisy/poseid documentation built on Aug. 22, 2019, 4:45 a.m.