Skip to content

Instantly share code, notes, and snippets.

View paulrougieux's full-sized avatar

Paul Rougieux paulrougieux

View GitHub Profile
@paulrougieux
paulrougieux / lmdisplay.R
Last active December 31, 2015 11:29
# Function to output latex equation based on an estimated model # The goal is to display an estimated equation in latex form with coefficients in parenthesis # Output can be used in an R Markdown file, provided the chunk option # {r, results='asis'} has been set
# Functions usefull to display estimated equations
# Author: Paul Rougieux
# Function to output LaTeX equation based on an estimated model
# The goal is to display an estimated equation in latex form with coefficients in parenthesis
# Output can be used in an R Markdown file, provided the chunk option
# {r, results='asis'} has been set
lmdisplay = function(model){
coefs = round(data.frame(t(summary(model)$coefficients[,1:2])),3)
@paulrougieux
paulrougieux / FAOregioncodes.R
Last active August 29, 2015 13:56
FAOregioncodes for the FAOSTAT module
FAOregioncodes <- structure(list(FAOST_CODE = c(5000L,
5100L, 5101L, 5102L, 5103L, 5104L, 5105L, 5200L, 5203L, 5204L,
5206L, 5207L, 5300L, 5301L, 5302L, 5303L, 5304L, 5305L, 5400L,
5401L, 5402L, 5403L, 5404L, 5500L, 5501L, 5502L, 5503L, 5504L,
5600L, 5706L, 5801L, 5802L, 5803L, 5815L, 5817L, 5848L, 5849L),
Region = structure(c(37L, 1L, 10L, 20L, 23L, 31L, 34L, 2L,
24L, 8L, 7L, 30L, 5L, 9L, 11L, 32L, 29L, 35L, 13L, 12L, 25L,
33L, 36L, 26L, 6L, 18L, 19L, 27L, 4L, 14L, 16L, 15L, 28L, 17L,
21L, 3L, 22L),
.Label = c("Africa", "Americas", "Annex I countries"
@paulrougieux
paulrougieux / list_files_mardown.R
Last active August 29, 2015 13:57
List of link to files in 2 level directory, useable in Markdown
```{r results='asis'}
for (f in list.files()){
if(file.info(f)$isdir){
l <- c(f, paste0(f,"/",list.files(f)))
cat(paste0("* [",c(f, list.files(f)),"]","(",l,")\n"))
}
}
```
@paulrougieux
paulrougieux / STATA.R
Last active August 29, 2015 14:16
Create and execute a STATA do file passing some variables as mustache tags
library(dplyr)
# Estimate a panel data model using plm and STATA
# Transmit some variables to STATA using mustache tags
# Write panel data to csv for use in STATA
write.csv(Grunfeld, path, item_, ".csv", names = FALSE, na = ".")
#' Create and execute a STATA do file
@paulrougieux
paulrougieux / dplyrsql.R
Last active August 29, 2015 14:20
Display SQL statement from dplyr chained operations
library(dplyr)
library(nycflights13)
# Create a SQLite databse
my_db <- src_sqlite("my_db.sqlite3", create = T)
flights_sqlite <- copy_to(my_db, flights,
temporary = FALSE,
indexes = list(c("year", "month", "day"),
"carrier", "tailnum"))
@paulrougieux
paulrougieux / octave.R
Last active August 29, 2015 14:23
Create and execute an Octave file
#' Create and execute an Octave file
#'
#' This function creates 2 files in the working directory:
#' \itemize{
#' \item{filename.m contains an Octave (Matlab) script}
#' \item{filename.csv contains estimation results}
#' }
#' Variables filename and
#' allitems are currently used as mustache tag variables.
#' More information on the Mustache format:
@paulrougieux
paulrougieux / knitr_iris.lyx
Last active September 17, 2015 08:17
knitr_iris
#LyX 2.1 created this file. For more info see http://www.lyx.org/
\lyxformat 474
\begin_document
\begin_header
\textclass article
\begin_preamble
\usepackage{booktabs}
\end_preamble
\options authoryear
@paulrougieux
paulrougieux / countwordpersentence.R
Last active October 30, 2015 15:14
Count the number of words per sentence
#' Separate the input text in sentences and count the number of words per sentence
#' @param blabla character vector containing a single text string.
#' @param minwordpersentence number minimum of word that a sentence should
#' have so that it is taken into account
#' @return a list countaining the number of sentence, wordcount,
#' mean number of words per sentence and total word count.
#' @export
countwordpersentence <- function(blabla, minwordpersentence = 2){
blabla <- strsplit(blabla,"\\.")[[1]]
@paulrougieux
paulrougieux / hyphen.lyx
Last active November 5, 2015 12:57
Hyphens are not printed when printing from the evince PDF viewer on Debian
#LyX 2.1 created this file. For more info see http://www.lyx.org/
\lyxformat 474
\begin_document
\begin_header
\textclass article
\use_default_options true
\begin_modules
knitr
\end_modules
\maintain_unincluded_children false
@paulrougieux
paulrougieux / knitrchunk
Last active November 19, 2015 09:21
Add a footnote in a table generated by xtable inside a Lyx document (answer from http://stackoverflow.com/a/13676007/2641825)
<<1stgenerationxtunitroot, results='asis'>>=
library(xtable)
x <- matrix(rnorm(60), ncol = 10)
x.big <- xtable(x,label='tabbig', caption='Example of longtable')
names(x.big) <- LETTERS[1:10]
names(x.big)[9] <- paste('I','footnote') # I put the tag on I letter
print(x.big,tabular.environment='longtable',floating=FALSE,
sanitize.text.function=function(str)gsub("footnote","\\footnote{my tricky footnote !!}",str,fixed=TRUE))
@