Thursday, February 23, 2012

Examples of the R function ddply in action

The ddply function in R is a really power tool. In this post I will show how it can be used by a few examples.

ddply takes three basic arguments
the input dataset
the splitting identifiers
the function to apply to the splitted datasets

Split data set in groups with identical id's and for each group produce one row containing the splitting variables and the values defined after the summarise parameter.
> timespan <- ddply(baseball, .(id), summarise, start = min(year), end = max(year)) 
> timespan[1:3, ]
id start end
1 abernte02 1955 1972
2 adairje01 1958 1970
3 adamsba01 1906 1926

Split data set in groups with identical id's and for transform each row in each of the resulting groups. The transform adds a new column to the original dataset.
> activeTime <- ddply(baseball, .(id), transform, experience = year - min(year)) 
> activeTime[1:10, ]
id year stint team lg g ab r h X2b X3b hr rbi sb cs bb so ibb hbp sh sf gidp experience
1 abernte02 1955 1 WS1 AL 40 26 1 4 0 0 0 0 0 0 0 6 0 0 4 0 1 0
2 abernte02 1956 1 WS1 AL 5 11 1 2 0 0 0 1 0 0 0 5 0 0 0 0 0 1
3 abernte02 1957 1 WS1 AL 26 24 3 4 1 0 0 1 0 0 1 5 0 1 2 0 2 2
4 abernte02 1960 1 WS1 AL 2 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 5
5 abernte02 1963 1 CLE AL 43 5 1 2 1 0 0 0 0 0 1 2 0 0 1 0 0 8
6 abernte02 1964 1 CLE AL 53 6 0 0 0 0 0 0 0 0 0 3 0 0 1 0 0 9
7 abernte02 1965 1 CHN NL 84 18 1 3 0 0 0 2 0 0 0 7 0 1 3 0 0 10
8 abernte02 1966 1 CHN NL 20 4 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 11
9 abernte02 1966 2 ATL NL 38 8 0 2 0 0 0 0 0 0 1 3 0 0 0 0 0 11
10 abernte02 1967 1 CIN NL 70 17 0 1 0 0 0 2 0 0 0 10 0 0 0 0 1 12

Get fraction of each group that have a value below a certain limit.
> careerBefore1960 <- ddply(baseball, .(id), summarise, fraction = ecdf(year)(1960))
> careerBefore1960[1:10, ]
id fraction
1 abernte02 0.2352941
2 adairje01 0.2000000
3 adamsba01 1.0000000
4 adamsbo03 1.0000000
5 adcocjo01 0.6470588

Thursday, April 14, 2011

Handle file permissions in shared git repository

When several people share their work on the same git repository, it is required to handle file permissions properly.
This is an ugly solution, but it seems to work

mkdir sharedrepo
cd sharedrepo
git init --bare
echo "chmod -f -R a+rw ." >> hooks/post-receive
chmod a+x hooks/post-receive
cd ..
chmod -R a+rw sharedrepo

Monday, November 9, 2009

Tools for version control

As a professional coder I use several tools. One of the most important of these tools is the version control system.

I use version control for keeping track of the source code of my programming projects. It makes it easy to mark when the code works, and track down changes when I notice that something is broken.
For several years I have used the version control system Subversion with satisfying results. Earlier this year I heard about another version control system named Git.
The main advantage of Git is that it is a decentralized version control system. This means that a network connection to the central repository is not required (which is the case for subversion).

To learn more about Git take a look at the references below.

Introduction to Git by Randal Schwartz, Youtube video (http://www.youtube.com/watch?v=8dhZ9BXQgc4).

Video podcasts about how to use git. Gitcasts (http://gitcasts.com/)

Homepage of Git (http://git-scm.com/)

Windows client for Git: gitextensions (http://gitextensions.sourceforge.net/)

A gentle git introduction
https://we.riseup.net/debian/git-development-howto

Using git without feeling stupid (part 1)
http://smalltalk.gnu.org/blog/bonzinip/using-git-without-feeling-stupid-part-1

Using git without feeling stupid (part 2)
http://smalltalk.gnu.org/blog/bonzinip/using-git-without-feeling-stupid-part-2

Good luck with your version control!

Thursday, February 5, 2009

Todonotes

In July the first version of the todonotes package were uploaded to the CTAN network [1]. After that I have received several requests for improvements, some have already been addressed and others are waiting to get time for implementing. To secure the source of the package I have started to use the github.com service, which allow open source projects to get free source code version control using the git software. The repository can be found at [2].

Best
Henrik

[1] http://www.tex.ac.uk/tex-archive/help/Catalogue/entries/todonotes.html
[2] http://github.com/henrikmidtiby/todonotes/tree/master

Monday, July 28, 2008

Todonotes version 2008-07-28

The todonotes package has now hit CTAN (the Comprehensive TeX Archive Network) and can be found at http://www.tex.ac.uk/tex-archive/help/Catalogue/entries/todonotes.html.


Recently I have received some ideas for improvements (thanks a lot!!!) of my todonotes package for latex. This has lead to a new version of the todonotes package with an improved handling of options (now for both the package and the individual todo command.)

The todonotes package lets you insert small notes in your latex document by the \todo{} command.

Options to the package:
  • disable
  • Make it possible to disable the functionality of the package. If this option is given, the
    commands \todo{} and \listoftodos are defined as commands with no effect. (But you can still
    compile you document with these commands).

  • colorinlistoftodos
  • Show small boxes in the list of todos with the color of the inserted todonotes.

  • dvistyle
  • The default style behaves bad when compiled with latex (some text placement problems). The dvistyle option, changes the visual behavior to avoid this text placement problem.

  • color
  • Sets the default background color of the todonotes

  • bordercolor
  • Sets the border color of the todonotes

  • textwidth
  • Sets the width of the todonotes

Options to the todo command:
  • color
  • Sets the background color of the todonote

  • size
  • Changes the relative size of the todonote

  • list / nolist (default: list)
  • Toggles whether the todo item is shown in the list of todos

  • inline / noinline (default: noinline)
  • Toggles inline view of the todonote

  • line / noline (default: line)
  • Toggles a line that connects the insertion point of the todonote with the note in the margin

Known issues:
  • Rendering differences between pdflatex and latex for items in the list of todos (latex do not break long lines, pdflatex does)

Source code and examples
todonotes.sty : http://pastebin.com/f749c073f (updated file) http://pastebin.com/f6a477b17 (original posted file)
exampledocument.tex : http://pastebin.com/f56db437a

Thursday, May 15, 2008

Updated todonotes package

The package has been updated, find the new version at http://midtiby.blogspot.com/2008/07/todonotes-version-2008-07-28.html.

In response to a request for making an option for the text width of the todonote, I have updated the todonotes package, including defining some additional options.

The package makes the following commands available
  • \todo{}
  • \listoftodos
The options the package understands are the following:
  • disable, if given the available commands have no effect
  • dvistyle, another layout of the todonotes which properly places the todo notes text when the document is compiled with latex and not pdflatex
  • color=value, sets the background color of the notes to value. Value is a color defined using the xcolor package
  • bordercolor=value, sets the border color of the notes to value
  • textwidth=value, sets the total width of the todonote to value. Value is a length.

I hope you'll find this useful. If you notice some rough edges / bugs let me hear, I might be able to get rid of them then.

Best
Henrik


The source code of the package is given below, and below of that the code for the sample document (the image at the top) is given.


% todonotes.sty
% ====================
% Part: Identification
% ====================
\ProvidesPackage{todonotes}[2008/05/15]


% ==============================
% Part: The package loading part
% ==============================
% Loads the packages that todonotes depends on.
\RequirePackage{ifthen}
\RequirePackage{xkeyval}
\RequirePackage{hyperref}
\RequirePackage{tikz}
\RequirePackage{calc}


% ==================
% Part: Initial code
% ==================
% Here the default values are set
\newcommand{\@backgroundcolor}{orange}
\newcommand{\@bordercolor}{black}
\newcommand{\@textwidth}{3cm}


% ================================
% Part: The declaration of options
% ================================
% In this part the various options for
% the package are defined.

% Make it possible to disable the functionality
% of the package. If this option is given, the
% commands \todo{} and \listoftodos are defined
% as commands with no effect. (But you can still
% compile you document with these commands).
\newif{\ifdisabled}
\DeclareOptionX{disable}{\disabledtrue}

% The default style behaves bad when compiled
% with latex (some text placement problems).
% The dvistyle option, changes the visual behavior
% to avoid this text placement problem.
\newif{\ifdviStyle}
\DeclareOptionX{dvistyle}{\dviStyletrue}

% Make the background color of the notes as
% an option.
\define@key{todonotes.sty}%
{color}{\renewcommand{\@backgroundcolor}{#1}}

% Make the color of the notes box color as
% an option.
\define@key{todonotes.sty}%
{bordercolor}{\renewcommand{\@bordercolor}{#1}}

% Make the text width as an option.
\define@key{todonotes.sty}%
{textwidth}{\renewcommand{\@textwidth}{#1}}

% Finally process the given options.
\ProcessOptionsX


% ========================
% Part: The main code part
% ========================
\ifdisabled
% If the option "disable" was passed to the package
% define two empty commands.
\newcommand{\listoftodos}{}
\newcommand{\todo}{}
\else
% Define the list of todos command
\newcommand{\listoftodos}
{\section*{Todo list} \@starttoc{tdo}}
\newcommand{\l@todo}
{\@dottedtocline{1}{0em}{2.3em}}

% Define styles used by the todo command
\tikzstyle{notestyle} = [
draw=\@bordercolor,
line width=0.5pt,
fill=\@backgroundcolor,
text width = \@textwidth - 1.6 ex - 1pt,
inner sep = 0.8 ex]
\tikzstyle{notestyleleft} = [notestyle]
\tikzstyle{connectstyle} = [
draw = \@backgroundcolor,
thick]

% Define the todo command
\newcommand{\todo}[1]{%
% Add to todo list
\addcontentsline{tdo}{todo}{\protect{#1}}%
%
% Remember where we are
\begin{tikzpicture}[remember picture, baseline=-0.75ex]%
\node [coordinate] (inText) {};%
\end{tikzpicture}%
%
% Make the margin par
\marginpar {% Draw note in right margin
\ifdviStyle%
% Using dviStyle
\tikz[remember picture] \draw node[notestyle] {}; \\ %
\begin{minipage}{\@textwidth}%
#1%
\end{minipage} \\%
\tikz[remember picture] \draw node[notestyle] (inNote) {};%
\begin{tikzpicture}[remember picture, overlay]%
\draw[connectstyle]%
([yshift=-0.2cm] inText)%
-| ([xshift=-0.2cm] inNote.west)%
-| (inNote.west);%
\end{tikzpicture}%
\else%
% Using normal style (non dviStyle)
\tikz[remember picture] \draw node[notestyle] (inNote) {#1};%
\begin{tikzpicture}[remember picture, overlay]%
\draw[connectstyle]%
([yshift=-0.2cm] inText)%
-| ([xshift=-0.2cm] inNote.west)%
-| (inNote.west);%
\end{tikzpicture}%
\fi % Ending \ifdviStyle
}%
}%
\fi % Ending \ifdisabled


And the code for the sample document


\documentclass{article}
\usepackage[colorlinks]{hyperref}
\usepackage{todonotes}

\begin{document}

\listoftodos

\section{The text}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam quam
quam, sodales vel, iaculis at, varius fringilla, urna. Cras urna.
\todo{A simple ting to do.}%
Morbi lorem eros, ullam\-corper id, egestas ac, interdum eu, nunc. Nam
enim eros, sodales at, dapibus eget, semper nec, augue. Morbi orci
\todo{Something much more difficult to explain. So it takes several
lines in the todo note and in the list of to do's.}%
orci, tincidunt ac, pellentesque vel, facilisis et, dolor. Nunc nec
mauris eu felis fringilla dictum.


Donec ornare, mauris in posuere
faucibus, lacus enim luctus nibh, vitae vestibulum sapien purus id
nisl. Morbi non dolor non odio ornare faucibus. Proin in purus.
\todo{There is still something to update in this document.}%
Vivamus nulla sem, eleifend ac, lacinia suscipit, facilisis a, est.
Mauris vitae purus. Donec ullamcorper, magna vitae sodales semper,
elit diam adipiscing neque, quis mattis ante orci a orci. Mauris eu
mauris non arcu pharetra euismod. Duis non elit. Etiam faucibus,
mauris quis mollis lobortis, mauris sem feugiat dolor, sit amet
porta libero lectus non erat. Donec cursus pede in tellus. Fusce
egestas. Aenean ut nisl.

Fusce eu arcu. Aliquam ultrices, turpis in facilisis lobortis, enim
massa bibendum ante, id blandit purus sapien non eros. Aenean
sodales ante eu magna. Proin vestibulum dolor auctor erat. Maecenas
at neque. Fusce ante odio, sollicitudin eget, cursus condimentum,
egestas in, urna.

Suspendisse rutrum eros. Phasellus suscipit velit
non nunc. Donec sollicitudin erat non dui. Aenean nec turpis. Aenean
pellentesque porta ipsum. Mauris eget nulla eu nunc pellentesque
sollicitudin. Pellentesque lacus. Fusce vehicula orci quis libero.
Vivamus consequat. Cum sociis natoque penatibus et magnis dis
parturient montes, nascetur ridiculus mus. Cras hendrerit elit. In
hac habitasse platea dictumst. Phasellus rhoncus massa non ipsum.
\end{document}

Thursday, April 24, 2008

Introduktionkursus til LaTeX

Den 30. april skal jeg afholde et introduktions kursus i latex for IDA på Ingeniør Højskolen i København. Her på denne side vil jeg samle henvisninger til de materialer der skal benyttes under kurset.


Forskellige ressourcer

Latex skabeloner
  1. Min nuværende latex skabelon, hent den her.
  2. En ældre skabelon, hent den her.
  3. En ældre skabelon til MAC, hent den her.
Latex referencer
  1. The not so short introduction to latex, link.
  2. Short math guide for latex, link.
  3. The comprehensive latex symbol list, link.
Slides til introduktionskurset
  1. Slides benyttet til kurset, hent det her.
  2. Kildekoden til de benyttede slides, hent det her.
Forskellige mindre latex opgaver
  1. Skrivning af matematik, hent det her.
  2. Overskrifter og indholdsfortegnelse, hent det her.
  3. Referencer og litteraturliste, hent det her.
  4. Lister i latex, hent det her.
  5. Spalter og figurer inde i teksten, hent det her.




Windows

Installing latex on windows
  1. Download MikTeX from here.
  2. Run the installer.
  3. Start -> All programs -> MikTeX ->
Installing a latex editor on windows

This requires that latex (eg. MikTeX) has been installed.
  1. Download TexnicCenter from: here
  2. Run the installer.
  3. Set path to your MikTeX installation (something like "C:\Programmer\MiKTeX 2.6\miktex\bin\")
  4. Select output profile: "LaTeX => PDF"
  5. Extract my template document and open the file doc\project.tex in TexnicCenter and try to compile it with pdflatex.
  6. During the compilation miktex will ask for permission to install some required packages (fancyhdr, listings, caption, subfig, pgf, xcolor, xkeyval)
  7. In case of trouble install the missing packages via: Start -> All program -> MikTeX 2.6 -> Browse packages
  8. Your document should now have been compiled, examine the generated .pdf file.

Linux

Installing latex on linux (ubuntu)
  1. Install the packages: texlive
Installing a latex editor on linux (ubuntu)
  1. Install kile