• Paris

  • Berlin

  • Chicago

  • Kiev

  • Palm Island

  • Rye

  • Venice

  • Sydney

  • Glasgow

  • Xitang

Convert a table to LaTeX

If you work in a scientific environment, you might work with LaTex. The question is how to get the table which we calculated with R to our LaTeX-paper without typing by hand.
One answer (as usual, there is always more than one way)  is to use the package “xtable”. To use this package, we have to install it first.

 install.packages("xtable")

And of course load it into our actual environment.

 library(xtable)

Now we can try it. Therefore we create a sample matrix result

result <- replicate(6, rnorm(10)) # random matrix

It should look similar, with different numbers, because we generated “random” numbers with rnorm:

            [,1]       [,2]        [,3]       [,4]       [,5]
 [1,]  1.5238250  0.6211481 -0.84720876  0.6180783 -0.1183714
 [2,] -0.8544256 -0.6308093 -0.72569442 -0.5762616  0.5040542
 [3,]  0.2988900 -1.5284343 -0.98393547 -1.5724599  0.3786788
 [4,]  1.9434472 -0.8336343  1.00989785  0.6229649 -0.7081008
 [5,]  0.9100517  1.1592287  0.08582132 -0.4478348 -2.0522663
 [6,] -0.4425697 -0.7471994 -0.22674705 -1.4229096  0.9369558
 [7,]  0.4013912 -0.7872627  1.48209638  0.2740836 -0.5614954
 [8,] -0.4750461 -0.4686828  0.80015906  1.2461592 -0.5779230
 [9,] -1.9628477  0.5800184  0.30853656  0.0465059  0.8173968
[10,]  1.2018598 -1.4571664  0.49775135 -0.2378211 -1.8798817

The LaTeX code can now be generated with

xtable(result)

The result should look like this:

% latex table generated in R 2.13.1 by xtable 1.6-0 package
% Thu Nov 10 14:00:07 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrrr}
  \hline
 & 1 & 2 & 3 & 4 & 5 \\
  \hline
1 & 1.52 & 0.62 & -0.85 & 0.62 & -0.12 \\
  2 & -0.85 & -0.63 & -0.73 & -0.58 & 0.50 \\
  3 & 0.30 & -1.53 & -0.98 & -1.57 & 0.38 \\
  4 & 1.94 & -0.83 & 1.01 & 0.62 & -0.71 \\
  5 & 0.91 & 1.16 & 0.09 & -0.45 & -2.05 \\
  6 & -0.44 & -0.75 & -0.23 & -1.42 & 0.94 \\
  7 & 0.40 & -0.79 & 1.48 & 0.27 & -0.56 \\
  8 & -0.48 & -0.47 & 0.80 & 1.25 & -0.58 \\
  9 & -1.96 & 0.58 & 0.31 & 0.05 & 0.82 \\
  10 & 1.20 & -1.46 & 0.50 & -0.24 & -1.88 \\
   \hline
\end{tabular}
\end{center}
\end{table}

This is the generic LaTeX code to generate a table in your paper. The standard settings will print just two digits. If we want to have more, we can tell xtable to write more. The command

xtable(result, digits=3)

gives us the same table but with three digits.
If you want, you can also print the table into a document. The command

print(xtable(result, digits=3), type="latex", file="output.tex")

does that for you.

LaTeX Table

Send R Output to a file

Sometimes, especially if you use a variable as a container for a lot of calculation results, e.g., if there are correlations to calculate for a set of variables, you want to save the output to a file to print it out or use it otherwise. For tables that is not a problem. Here we can use the write command in R. But what to do if there is a list saved in the variable?
Here we can use the command sink(). Like pdf(), sink() sends the output of the following commands to a file.

So

sink("sink-examp.txt")
i <- 1:10
outer(i, i, "*")
sink()

writes

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    2    3    4    5    6    7    8    9    10
 [2,]    2    4    6    8   10   12   14   16   18    20
 [3,]    3    6    9   12   15   18   21   24   27    30
 [4,]    4    8   12   16   20   24   28   32   36    40
 [5,]    5   10   15   20   25   30   35   40   45    50
 [6,]    6   12   18   24   30   36   42   48   54    60
 [7,]    7   14   21   28   35   42   49   56   63    70
 [8,]    8   16   24   32   40   48   56   64   72    80
 [9,]    9   18   27   36   45   54   63   72   81    90
[10,]   10   20   30   40   50   60   70   80   90   100

to the file.

Mobile in Urban Space

The industrial age has radically changed our cities. Especially with the car as a means of mass transportation, the obvious coexistence of different areas of life has disintegrated and the structure of the city has literally melted away. How can we reclaim the city as a habitat?

With concrete examples Wolfgang Christ discusses his approaches to urban/metropolitan areas. He developed, jointly with the Eco Institute in Freiburg, the concept of the ”environmental railway station” (Umweltbahnhof) in Grünstadt northwest of Ludwigshafen, where different transportation methods were integrated.

The event is free of charge, but a registration is required:

http://www.daimler-benz-stiftung.de/cms/index.php?page=lesung-haus-huth-anmeldung

When: October 10, 2011, 06:00 pm
Where: Haus Huth, Alte Potsdamer Str. 5, 10785 Berlin

Databases in R

For some cases it is helpful to store the data not in a file, but in a database. Databases have some advantages when it comes to a large amount of data. The most important factor is that for calculations, just the actual data that is needed for the calculation needs to be loaded in the random access memory (RAM). Another advantage is the possibility to run calculations (stored procedures) with some database engines which will speed up some complex calculations with large data sets, as well as the abolition of exporting the results for other programs e.g. to plot the data with a GIS System. A very good and easy way to implement the database connection in R is with RODBC.
Read more...

Welcome to my New Website

After a couple months of hard work, I am proud to present the website in a new design as well as a new software system. As a result, I rearranged the pages and categorized them in a new way. As part of the revision, I switched this site completely to English.

In the  future, I will extend this site to include the topics “geographic-related statistics with R and their graphical presentation” and “simulation of urban systems” as well as some new features.