Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauricio Vargas committed Feb 20, 2018
0 parents commit e5daec1
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
17 changes: 17 additions & 0 deletions DESCRIPTION
@@ -0,0 +1,17 @@
Package: economiccomplexity
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <yourself@somewhere.net>
Description: More about what it does (maybe more than one line)
Use four spaces when indenting paragraphs within the Description.
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Imports:
magrittr,
dplyr,
tidyr,
stats
RoxygenNote: 6.0.1
13 changes: 13 additions & 0 deletions NAMESPACE
@@ -0,0 +1,13 @@
# Generated by roxygen2: do not edit by hand

export(rca)
importFrom(dplyr,distinct)
importFrom(dplyr,filter)
importFrom(dplyr,group_by)
importFrom(dplyr,mutate)
importFrom(dplyr,select)
importFrom(dplyr,select_)
importFrom(dplyr,ungroup)
importFrom(magrittr,"%>%")
importFrom(stats,setNames)
importFrom(tidyr,unite)
49 changes: 49 additions & 0 deletions R/rca.R
@@ -0,0 +1,49 @@
globalVariables(
c(
"pairs",
"select",
"sum_c_p_xcp",
"sum_c_xcp",
"sum_p_xcp",
"xcp"
)
)

#' Computes RCA (Revealed Comparative Advantage)
#'
#' @export
#' @param data a tibble (or data frame) in long format
#' @param c name of column that contains countries (by default is "country")
#' @param p name of column that contains products (by default is "product")
#' @param value name of column that contains trade values (by default is "export_val")
#' @importFrom magrittr %>%
#' @importFrom dplyr select_ select mutate group_by ungroup filter distinct
#' @importFrom tidyr unite
#' @importFrom stats setNames
#' @examples
#' # Demo dataset 'Fantasy World'
#' rca(fantasy_world_long)
#' @keywords functions

rca <- function(data = data, c = "country", p = "product", value = "export_val") {
data_rca <- data %>%
select_(.dots = c(c, p, value)) %>%
setNames(c("c", "p", "value")) %>%
unite(pairs, c, p, remove = FALSE) %>%
group_by(p) %>% # Sum by categories in P
mutate(sum_p_xcp = sum(value, na.rm = TRUE)) %>%
ungroup() %>%
group_by(c) %>% # Sum by categories in C
mutate(sum_c_xcp = sum(value, na.rm = TRUE)) %>%
ungroup() %>%
group_by(pairs) %>% # Sum by pairs (C,P)
mutate(xcp = sum(value, na.rm = TRUE)) %>%
ungroup() %>%
distinct(pairs, .keep_all = TRUE) %>%
select(-pairs) %>%
filter(sum_c_xcp > 0) %>%
mutate(sum_c_p_xcp = sum(xcp, na.rm = TRUE)) %>% # Total sum
mutate(rca = (xcp / sum_c_xcp) / (sum_p_xcp / sum_c_p_xcp)) %>% # Compute RCA
select(c, p, value, rca)
return(data_rca)
}
Binary file added data/fantasy_world_long.rdata
Binary file not shown.
Binary file added data/fantasy_world_matrix.rdata
Binary file not shown.
21 changes: 21 additions & 0 deletions economiccomplexity.Rproj
@@ -0,0 +1,21 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace,vignette
25 changes: 25 additions & 0 deletions man/rca.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e5daec1

Please sign in to comment.