Skip to content

Commit

Permalink
version 1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
fbellelli authored and cran-robot committed Sep 1, 2023
0 parents commit 36025cd
Show file tree
Hide file tree
Showing 83 changed files with 8,846 additions and 0 deletions.
31 changes: 31 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Package: countries
Type: Package
Title: Deal with Country Data in an Easy Way
Version: 1.0.3
Authors@R:
person("Francesco Saverio", "Bellelli", , "fsabellelli@gmail.com", role = c("aut", "cre", "cph"),
comment = c(Website = "https://fbellelli.com/"))
Description: Wrangle country data more effectively and quickly.
This package contains functions to easily identify and convert country names,
download country information, merge country data from different sources,
and make quick world maps.
License: GPL-3
Encoding: UTF-8
RoxygenNote: 7.2.3
Imports: stringdist, tidyr, stringr, dplyr, knitr, fastmatch,
lubridate, utils, methods, stats, httr, jsonlite, ggplot2,
viridis, grDevices
Suggests: data.table, rmarkdown, testthat
Depends: R (>= 2.10)
VignetteBuilder: knitr
URL: https://fbellelli.github.io/countries/,
https://github.com/fbellelli/countries
BugReports: https://github.com/fbellelli/countries/issues
Config/testthat/edition: 3
LazyData: true
NeedsCompilation: no
Packaged: 2023-09-01 13:31:41 UTC; FSB
Author: Francesco Saverio Bellelli [aut, cre, cph] (https://fbellelli.com/)
Maintainer: Francesco Saverio Bellelli <fsabellelli@gmail.com>
Repository: CRAN
Date/Publication: 2023-09-01 15:50:02 UTC
82 changes: 82 additions & 0 deletions MD5
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
b947eb5cdb99f4e362d6b6b5a1ca8b4e *DESCRIPTION
a95ab0a827028110692a7dae2aa99101 *NAMESPACE
0e6122e6aa53c9d8e83c9c47e1bbc3ae *NEWS.md
44d2cae832bf363be8ac99818104adc8 *R/auto_melt.R
3438b2c41e9c5a049d9b31e0714995d3 *R/auto_merge.R
b263764e99818915c3af9f626a0bc694 *R/check_wide_format.R
d19385c957b0aebccd98e06d0a002099 *R/country_info.R
e7af7478b451016330354b213dab11e5 *R/country_name.R
9d42485df3c3364640c7884eae4f95e5 *R/data_long.R
988b9c53e5c74cbcc7e34f576383c778 *R/data_wide.R
d68c0805e97fddf25e5845f6e0acfa2f *R/data_world.R
a48310b1e0ab29b8d167c09cd2c0f3e6 *R/find_countrycol.R
63f5b06dd9bfd99b659dc9ed76f5cbfb *R/find_keycol.R
341fdd3989356adaafec9f49047ba9a4 *R/find_timecol.R
b2350fbae252c2e2b469fefe2a211e0a *R/has.invalid.multibyte.string.R
71e679eb2e99de151bcc99379a221851 *R/is.yearcol.R
e9377c4e7fec1342928925b45985c09d *R/is_country.R
7462073eb90f69ca8c86a8574c3a58b6 *R/is_date.R
9b1fa35aec228a0511cd32ddf57674c0 *R/is_keycol.R
ef073b1907f50c76632b62e95c098bf3 *R/list_fields.R
b7c879f029d3c13a69879f5b16aa3626 *R/map_palettes_and_themes.R
903deb275bbbe443a65c0f6db06b8904 *R/match_table.R
2f85ce9471994478ff99642ee385f953 *R/mode.R
99f98f0605b128410f1bb9139b9d018e *R/parse.by.order.R
5a37546d74ead984da1179870df4876c *R/quick_map.R
97cb01db7d9786585a3ae48dbcebed68 *R/sysdata.rda
8aa6f00a0f289c0cb7a690d701708e6f *R/which_min-max-mode.R
a7bdc2e35aef141bb473fb4719f4fe6f *README.md
e2f048aeb1ff5b9f5ad7f33c914ad454 *build/vignette.rds
1e375b313bfda9eb653a05ffd9924cda *data/country_reference_list.rda
0cf365f67f04b8a449ae14008f43f268 *data/country_reference_list_long.rda
1f0663373d69793da7b98914a2a9ed16 *data/world.rda
71b16632b064c42439c8c869a1f639e3 *inst/doc/auto_merge.R
c5cfe6ac0e8888634d23da4f1dfec0be *inst/doc/auto_merge.Rmd
1048f4c99ba33ecd0f55bdac05ff0a9f *inst/doc/auto_merge.html
dc91c637da076712507555f7d80a32c3 *inst/doc/data_structure.R
d876d0ec1d539156ca867838b51583ed *inst/doc/data_structure.Rmd
6247c039b19eb7f8dd1ae76f35599d38 *inst/doc/data_structure.html
ee51f9ba307b28bb2acaf315a81a06cb *inst/doc/dealing_with_names.R
0088a25ad588b8d95628009bd1e63555 *inst/doc/dealing_with_names.Rmd
820e1a3ba367d48dad727f95169b0afe *inst/doc/dealing_with_names.html
7356195d829dfe56994b6b58b3e46d92 *inst/doc/quick_map.R
19e68c3cc34e35e63a7d46c4ff215d6e *inst/doc/quick_map.Rmd
e1e8493cee4063f1868b6e726c35f05f *inst/doc/quick_map.html
0c1d510e5c29173f65fd62d049283568 *man/Mode.Rd
a733f14d4c729e69d74ce50312799e02 *man/auto_melt.Rd
b35bafe1f838b98969a0be6c59e3541e *man/auto_merge.Rd
260f8778a234d1cac6e8299e242da865 *man/check_wide_format.Rd
6b71ecb50bc5d274c7b337626450b041 *man/country_info.Rd
898bd98ac226fb73346b92e4f06ad9ef *man/country_name.Rd
5e57f2c0290c218a1240048c240aacdd *man/country_reference_list.Rd
f02da7d9aea610e9ccda0635a01966fb *man/country_reference_list_long.Rd
424a813073100dd310c469f68479c26b *man/figures/hexagon_sticker.png
b5f9fc0a848db6df188fdc5105b17da4 *man/figures/hexagon_sticker_medium.png
bc47056edb7eeac741270b1b2d79cdb9 *man/figures/hexagon_sticker_small.png
45b6157deed9af890cc51e0ab543d912 *man/figures/logo.ico
424a813073100dd310c469f68479c26b *man/figures/logo.png
753bbeaa9b2ad33cf3b30070d7b82640 *man/figures/logo16.png
db0dfec88da3d905dfee7c6777f08590 *man/figures/logo192.png
a98df5fd85768c6cfc8628b3dc9e8804 *man/figures/logo32.png
ccde4c9d7d4b054cb26abf8562e9ccbe *man/figures/logo512.png
8aeb931627b303aba0a2f9e5f10c7d85 *man/figures/unnamed-chunk-6-1.png
8aeb931627b303aba0a2f9e5f10c7d85 *man/figures/unnamed-chunk-7-1.png
32a6fc920449ad4dbc75bc9d02bf9643 *man/figures/unnamed-chunk-8-1.png
56b0845cc9b76c81dbf0a11a3ba815d5 *man/find_countrycol.Rd
5f3b9a15883654298e1cbba40836379a *man/find_keycol.Rd
5db241def4048d2c74a1efc01b659a62 *man/find_timecol.Rd
0c794ac131b6be7559b8b61a1591e1af *man/is_country.Rd
3ecf8ab63ced7b4571ca8f1783767860 *man/is_date.Rd
ced0bb9411b00bc0afce4b890f4d770b *man/is_keycol.Rd
6ff85ebe41a3f090d64a7558a8436b2d *man/list_fields.Rd
1a055bcc57e9650f6c69039c43e6ba04 *man/match_table.Rd
f2a1108aba0a4c616b2d8e6ec166afa3 *man/palettes_countries.Rd
e8da43942f05cb2a582d09439cf0f3ee *man/quick_map.Rd
6c86f0a89214839bb4b6939a84f00eb6 *man/which_min.Rd
8d0533094c347962433a572f9c95d329 *man/world.Rd
5250cf6b34d84721cdb2683b313e85ad *tests/testthat.R
2d3a8fcf9094d36f345bdc195a5007f1 *tests/testthat/test_function_output.R
c5cfe6ac0e8888634d23da4f1dfec0be *vignettes/auto_merge.Rmd
d876d0ec1d539156ca867838b51583ed *vignettes/data_structure.Rmd
0088a25ad588b8d95628009bd1e63555 *vignettes/dealing_with_names.Rmd
19e68c3cc34e35e63a7d46c4ff215d6e *vignettes/quick_map.Rmd
25 changes: 25 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by roxygen2: do not edit by hand

export(Mode)
export(auto_melt)
export(auto_merge)
export(check_wide_format)
export(country_info)
export(country_name)
export(find_countrycol)
export(find_keycol)
export(find_timecol)
export(is_country)
export(is_date)
export(is_keycol)
export(list_fields)
export(match_table)
export(palettes_countries)
export(quick_map)
export(which_max)
export(which_min)
export(which_mode)
import(ggplot2)
importFrom(fastmatch,"%fin%")
importFrom(stats,na.omit)
importFrom(stats,quantile)
41 changes: 41 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# countries 1.0.3
* Fixed hyperlinks in for resubmission
* Eliminated references to unexported objects in documentation

# countries 1.0
* First CRAN submission

# countries 0.3

* Added function *country_info()* to download information about countries (e.g. capital city, currency, neighbouring countries, etc.)
* Added function *list_fields()* returning a list of accepted fields for *country_info()*
* Added function *auto_merge()* to quickly join multiple dataset and automatically deal with country names and time columns
* Added function *auto_melt()* to automatically pivot tables containing country names and years in their headers
* Added function *quick_map()* to easily plot coloured country maps
* Added function *palettes_countries()* providing access to the package's colour themes
* Added vignette on plotting chloropleth maps with *quick_map* and merging country data with *auto_merge()*
* Bug fixes

# countries 0.2

* Added function *is_country()* to test whether a string is a country name or related to list of countries names
* Added function *is_date()* to test whether values are dates
* Added function *is_keycol()* to test whether a set of columns could be the keys of a table
* Added function *find_countrycol()* to automate search of columns containing country names in data frames
* Added function *find_timecol()* to automate search of date and/or year columns in data frames
* Added function *find_keycol()* to automate search of table keys in data frames
* Added functions *which_min()*, *which_max()* and *which_mode()* returning all the positions of vector's minimum, maximum and mode values.
* Speed improvements for the functions *country_name()* and *match_table()*
* Bug fixes
* Added vignette on data structure



# countries 0.1

* Function *country_name()* for converting and translating country names based on a fuzzy matching approach
* Function *match_table()* to easily construct conversion tables with the help of fuzzy matching algorithm
* Function *Mode()* returns the statistical mode of a vectors (most frequent observation)
* Dataset *country_reference_list* containing a country names in different languages and naming conventions
* Added vignettes and documentation
* Created website for the package
95 changes: 95 additions & 0 deletions R/auto_melt.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#' @title Automatic pivoting of country and year columns to a long format
#'
#' @description
#' When at least 3 country names or years are found in the column names, the function will automatically transform the table from a wide to a long format by pivoting the country/year columns.
#' This is equivalent to applying \code{tidyr::pivot_longer()} or \code{data.table::melt()} on the columns with years or countries as names.
#' The function is able to detect years also when they are preceded by a prefix.
#'
#' @param x A data.frame object to check and pivot country or year columns.
#' @param names_to String indicating how the column holding the name of the pivoted columns should be called in the output table. Default is \code{"pivoted_colnames"}
#' @param values_to String indicating how the column containing the values of the pivoted columns should be called in the output table. Default is \code{"pivoted_data"}
#' @param verbose Logical value. If set to \code{TRUE} (the default), a message will be displayed on the console indicating which columns are being pivoted. If set to \code{FALSE}, the messages are turned off.
#' @param pivoting_info Logical value indicating whether to return the list of names of the column that have been pivoted. Default is \code{FALSE}. If set to \code{TRUE}, the output will be a list instead of simple data.frame. Teh list will contain 1) the pivoted table, 2) the list of pivoted columns.
#' @returns A table transformed into a "long" format by pivoting country or year columns. If year columns are found, a numeric column called \code{"year_pivoted_colnames"} is added isolating the years extracted from the table header's.
#' @seealso \link[countries]{auto_merge}, \link[countries]{find_countrycol},\link[countries]{find_timecol}
#' @export
#' @examples
#' # example data
#' example <- data.frame(Date = c("01.01.2019", "01.02.2019", "01.03.2019"),
#' Japan = 1:3,
#' Norway = 2:4,
#' Germany = 3:5,
#' US = 4:6)
#' example2 <- data.frame(Sector = c("Agriculture", "Mining", "Forestry"),
#' X2000 = 1:3,
#' X2001 = 2:4,
#' X2002 = 3:5,
#' X2003 = 4:6)
#'
#' # examples pivotting countries and years from column names
#' auto_melt(example)
#' auto_melt(example2)
auto_melt <- function(
x,
names_to = "pivoted_colnames",
values_to = "pivoted_data",
verbose = TRUE,
pivoting_info = FALSE
){

# check inputs
if(!is.data.frame(as.data.frame(x))|is.null(x)){stop("Argument - x - needs to be a dataframe")}
if (is.na(names_to)|is.null(names_to)) stop("The argument - names_to - cannot be NA or NULL. It needs to be a string.")
if (is.na(values_to)|is.null(values_to)) stop("The argument - values_to - cannot be NA or NULL. It needs to be a logical value")
if (is.na(verbose)|is.null(verbose)) stop("The argument - verbose - cannot be NA or NULL. It needs to be a logical value")
if (is.na(pivoting_info)|is.null(pivoting_info)) stop("The argument - pivoting_info - cannot be NA or NULL. It needs to be a string.")
if (!is.character(names_to)|length(names_to)!=1)stop("The argument - names_to - is invalid. It needs to be a string.")
if (!is.character(values_to)|length(values_to)!=1)stop("The argument - values_to - is invalid. It needs to be a string.")
if (!is.logical(verbose) | length(verbose)!=1) stop("Function argument - verbose - needs to be a logical statement (TRUE/FALSE)")
if (!is.logical(pivoting_info) | length(pivoting_info)!=1) stop("Function argument - pivoting_info - needs to be a logical statement (TRUE/FALSE)")

# convert input to data.frame
data <- as.data.frame(x)

#check column names for countries/years
temp <- check_wide_format(data)

# proceed if there is any column to pivot
if (!is.null(temp)){

# pivot table if countries or years were found and adjust name
data <- as.data.frame(tidyr::pivot_longer(data, dplyr::all_of(temp$col_name), names_to = names_to, values_to = values_to))

# move pivoted keys to front of table
data <- data[, c(names_to, colnames(data)[colnames(data) != names_to])]

# convert year to numeric if possible, otherwise add numeric column with detected year in string
if (colnames(temp)[1] == "year"){
if (all(grepl('^(?=.)([+-]?([0-9]*)(\\.([0-9]+))?)$', temp$col_name, perl = TRUE))){
data[, names_to] <- as.numeric(data[, names_to])
} else {
data[,"year_pivoted_colnames"] <- temp[fastmatch::fmatch(data[, names_to], temp$col_name), 1]
}
}

# save name of pivoted columns
pivoted_cols <- as.vector(temp$col_name)

# issue message to console
if (verbose) cat(paste0(colnames(temp)[1], " detected in column names, pivoting columns: ", paste(temp$col_name[1:3], collapse=", ", sep = ""), if (nrow(temp)>3){paste0(", ..., ", temp$col_name[nrow(temp)])}, "\n"))
} else {

pivoted_cols <- NULL

if (verbose) cat("No column was pivoted\n")
}

# final output
if (pivoting_info){
return(list(output = data,
pivoted_cols = pivoted_cols))
} else {
return(data)
}

}

0 comments on commit 36025cd

Please sign in to comment.