Skip to content

Commit 5509ff0

Browse files
committed
update authors
1 parent 6ceba81 commit 5509ff0

17 files changed

+217
-25
lines changed

‎DESCRIPTION‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Authors@R: c(
1010
comment = "Josep reviewed the package for rOpenSci, see https://github.com/ropensci/software-review/issues/623#issuecomment-1951446662"),
1111
person("Le Meur", "Nolwenn", role = "rev",
1212
comment = "Nolwenn reviewed the package for rOpenSci, see https://github.com/ropensci/software-review/issues/623#issuecomment-1961501137"),
13-
person("Filipe", "Batista e Silva", role = "ctb",
13+
person("Filipe", "Batista e Silva", role = "aut",
1414
comment = c(ORCID = "0000-0002-8752-6464")),
15-
person("Carmelo", "Attardo", role = "ctb"),
16-
person("Cristian", "Pigaiani", role = "ctb"),
17-
person("Michele", "Tucci", role = "ctb")
15+
person("Carmelo", "Attardo", role = "aut"),
16+
person("Cristian", "Pigaiani", role = "aut"),
17+
person("Michele", "Tucci", role = "aut")
1818
)
1919
Description: Motivated by changing administrative boundaries over time,
2020
the 'nuts' package can convert European regional data with NUTS codes

‎data-raw/.RData‎

9.15 KB
Binary file not shown.

‎data-raw/run_ropensci_nuts_in.do‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
*net install github, from("https://haghish.github.io/github/")
2+
*github install haghish/rcall
3+
4+
5+
// Run R code directly
6+
rcall: print("Hello from R")
7+
8+
// Import data
9+
rcall: library(nuts)
10+
rcall: data(manure)
11+
rcall: manure
12+
rcall: summary(manure)
13+
14+
rcall: write_dta(manure, "manure.dta")
15+
16+
rcall: library(dplyr)
17+
rcall: library(haven)
18+
rcall: setwd("/home/moritz/dev/nuts/data-raw/")
19+
20+
21+
22+
23+
library(nuts)
24+
library(dplyr)
25+
library(haven)
26+
setwd("/home/moritz/dev/nuts/data-raw/");
27+
data(manure)
28+
29+
30+
31+
32+
cd /home/moritz/dev/nuts/data-raw/
33+
sysuse auto, clear
34+
describe, full
35+
save myauto.dta, replace
36+
rsource, terminator(END_OF_R)
37+
library(haven);
38+
setwd("/home/moritz/dev/nuts/data-raw/");
39+
rauto<-read_dta("myauto.dta", convert.f=TRUE);
40+
rauto;
41+
attributes(rauto);
42+
q();
43+
END_OF_R
44+
45+
46+
47+
# Load packages
48+
library(nuts)
49+
library(dplyr)
50+
library(stringr)
51+
52+
# Loading and subsetting Eurostat data
53+
data(patents, package = "nuts")
54+
55+
pat_n2 <- patents %>% filter(nchar(geo) == 4) # NUTS-2 values
56+
57+
pat_n2_mhab_12_no <- pat_n2 %>%
58+
filter(unit == "P_MHAB") %>% # Patents per one million inhabitants
59+
filter(time == 2012) %>% # 2012
60+
filter(str_detect(geo, "^NO")) %>% # Norway
61+
dplyr::select(-unit)
62+
63+
# Classifying the Data
64+
pat_classified <- nuts_classify(
65+
data = pat_n2_mhab_12_no,
66+
nuts_code = "geo"
67+
)
68+
69+
70+
pat_classified <- nuts_classify(
71+
data = pat_n2,
72+
nuts_code = "geo"
73+
)

‎data-raw/run_ropensci_nuts_in.py‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# NUTS R PACKAGE IN PYTHON
2+
#============================
3+
# Use the rOpenSci R package nuts for the conversion of European NUTS codes in Python with the help of this gist.
4+
# More information on the package can be found here: https://docs.ropensci.org/nuts/
5+
#
6+
# [Remark] It may be easier to run this in a conda environment created with:
7+
# conda create -n r-env r-base r-essentials
8+
# conda activate r-env
9+
# pip install rpy2
10+
11+
# Import modules
12+
import rpy2.robjects as ro
13+
from rpy2.robjects.packages import importr
14+
from rpy2.robjects import pandas2ri
15+
16+
# Load nuts package
17+
nuts = importr('nuts')
18+
19+
# Load required packages
20+
ro.r('''
21+
library(nuts)
22+
library(dplyr)
23+
''')
24+
25+
# Load some Eurostat data that ships with the nuts package
26+
ro.r('''
27+
data(manure)
28+
''')
29+
manure_df = ro.r('manure')
30+
31+
# Convert to pandas
32+
with ro.conversion.localconverter(ro.default_converter + pandas2ri.converter):
33+
manure_df = ro.conversion.rpy2py(manure_df)
34+
35+
# Let's only look at NUTS 3 codes (when number of digits in NUTS codes is 5)
36+
manure_df = manure_df[manure_df['geo'].str.len() == 5]
37+
38+
# The indicator I07A_EQ_Y measures the number of manure storage holdings
39+
manure_df = manure_df[manure_df['indic_ag'] == 'I07A_EQ_Y']
40+
manure_df = manure_df.drop(['indic_ag'], axis=1)
41+
42+
# Focus on the year 2010
43+
manure_df = manure_df[manure_df['time'] == 2010]
44+
print(manure_df)
45+
46+
47+
# 1. DETECT NUTS VERSIONS
48+
#--------------------------
49+
# Let's explore how NUTS versions can be found in this Eurostat dataset
50+
51+
# Pass your pandas data frame to R for the conversion
52+
with ro.conversion.localconverter(ro.default_converter + pandas2ri.converter):
53+
ro.globalenv['manure_df'] = manure_df
54+
55+
# Convert manure holdings in dataset to level of NUTS codes 2021 version
56+
results = ro.r('''
57+
nuts_classify(nuts_code = "geo", data = manure_df)
58+
''')
59+
60+
# Convert result back to pandas
61+
with ro.conversion.localconverter(ro.default_converter + pandas2ri.converter):
62+
manure_classified_df = ro.conversion.rpy2py(results)
63+
64+
# 1. This is the original data set augmented with three variables:
65+
# from_version = detected NUTS version(s)
66+
# from_level = detected NUTS level
67+
# country = detected country
68+
print(manure_classified_df[0])
69+
70+
# 2. Breakdown of the overlap with each NUTS version
71+
print(manure_classified_df[1])
72+
73+
# 3. Missing NUTS codes
74+
print(manure_classified_df[2])
75+
76+
77+
# 2. CONVERT NUTS VERSIONS
78+
#--------------------------
79+
# Let's convert manure holdings in dataset to level of NUTS codes 2021 version
80+
results = ro.r('''
81+
manure_df %>%
82+
# We first classify the NUTS codes
83+
nuts_classify(nuts_code = "geo") %>%
84+
# And then convert
85+
nuts_convert_version(to_version = '2021',
86+
weight = 'pop11',
87+
variables = c('values' = 'absolute'))
88+
''')
89+
90+
# Convert result back to pandas
91+
with ro.conversion.localconverter(ro.default_converter + pandas2ri.converter):
92+
manure_converted_df = ro.conversion.rpy2py(results)
93+
94+
# Voila, the converted data
95+
print(manure_converted_df)
96+
97+
98+
# 3. AGGREGATE NUTS LEVELS
99+
#--------------------------
100+
# Let's aggregate NUTS 3 to NUTS 2
101+
results = ro.r('''
102+
manure_df %>%
103+
# We first classify the NUTS codes
104+
nuts_classify(nuts_code = "geo") %>%
105+
# And aggregate
106+
nuts_aggregate(to_level = 2,
107+
variables = c('values' = 'absolute'))
108+
''')
109+
110+
# Convert result back to pandas
111+
with ro.conversion.localconverter(ro.default_converter + pandas2ri.converter):
112+
manure_aggregated_df = ro.conversion.rpy2py(results)
113+
114+
# The data aggregated to nuts level 2
115+
print(manure_aggregated_df)

‎docs/404.html‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/LICENSE-text.html‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/LICENSE.html‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/articles/index.html‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/authors.html‎

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/index.html‎

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)