I want to share a function I wrote for my dissertation. The function is useful for putting up to two R tables into one TeX table.

You have to load the package 'languageR' to have the dataset 'dative' available.

Let's suppose you have two tables, one with means and another one with standard deviations. Of course, these two tables have the same number of rows and columns - this is also checked in the function.


library(languageR)

mean.tab <- tapply(dative$LengthOfTheme, list(dative$PronomOfTheme, dative$AnimacyOfTheme), FUN = mean)

sd.tab <- tapply(dative$LengthOfTheme, list(dative$PronomOfTheme, dative$AnimacyOfTheme), FUN = sd)

The tables look like this:

> mean.tab
               animate inanimate
nonpronominal 3.365854  4.588718
pronominal    2.000000  2.270619
> sd.tab
               animate inanimate
nonpronominal 3.440611  4.493893
pronominal    2.715695  2.621017

But what you want, is this:








(click to enlarge)

As TeX code, this looks like this:

\begin{table}
  \centering
  \caption{Mean (SD) lengths of theme.}
  \begin{tabularx}{\textwidth}{X X X X}
  \toprule
   & & \multicolumn{2}{c}{Animacy of Theme} \\
   & & animate & inanimate \\
   \midrule
  \multirow{2}{*}{Pronominal}& no & \SI{3{,}37}{letters} (\SI{3{,}44}{}) & \SI{4{,}59}{letters} (\SI{4{,}49}{}) \\
  & yes & \SI{2}{letters} (\SI{2{,}72}{}) & \SI{2{,}27}{letters} (\SI{2{,}62}{}) \\
  \bottomrule
  \end{tabularx}
\label{yourlabel}
\end{table}

Most of the time, you have your raw code for the table without the values. The part that sucks is the typing of all the numbers, brackets, backslashes for the actual values in the table. This part is done by my function 'make.tex.vals' (see the bottom of the text).

A few more comments: In the TeX table, I used the \toprule, \midrule and \bottomrule commands. These are from the TeX package 'booktabs' and allow for APA style tables. The \SI{...} commands are from the package 'siunitx' and allow for good value-unit formatting. The function provided below creates output according to this package and fills in the values and units for you.

The function takes the following arguments:
tab: The first table to convert.
dig: The number of digits the values are rounded to (default: 1)
unit1: The unit of the values in the first table (default: "s" for seconds)
dec: The decimal separator (default: "," for German style numbers)
prefix1: A string to put before each value from 'tab' (default: "" for no prefix)
suffix1: Same as 'prefix1' but after each value
tab2: The second table, the values in each cell are written behind the values of 'tab' in the respective cell (default: NULL, no second table)
unit2: See 'unit1'
prefix2: See 'prefix1' (default: "(", e.g., for standard deviations)
suffix2: See 'suffix1' (default: ")")
eor: End of row marker (default: "\\\\", two backslashes, makes it easier to copy the whole line of output from R to TeX)

So, if I do a call like this:
make.tex.vals(tab = mean.tab, dig = 2, unit1 = "letters", dec = ",", tab2 = sd.tab)

I get this output on the R console:
\SI{3{,}37}{letters} (\SI{3{,}44}{}) & \SI{4{,}59}{letters} (\SI{4{,}49}{}) \\
\SI{2}{letters} (\SI{2{,}72}{}) & \SI{2{,}27}{letters} (\SI{2{,}62}{}) \\


I simply copy this into my raw table and saved myself a lot of typing.

Leave a comment if you have any questions regarding the use of the function...

Now, here is the function:

make.tex.vals <- function (tab, dig = 1, unit1 = "s", dec = ",", prefix1 = "", suffix1 = "", tab2 = NULLunit2 = "", prefix2 = "(", suffix2 = ")", eor = " \\\\") {
  dec.sep <- paste("{", dec, "}", sep = "")
  if (!is.null(tab2)) {
    stopifnot(all.equal(dim(tab), dim(tab2)))
    for (row.i in 1:nrow(tab)) {
      row.tab1 <- tab[row.i,]
      row.tab2 <- tab2[row.i,]
      vals <- c()
      for (val.i in 1:length(row.tab1)) {
        val1 <- as.character(round(row.tab1[val.i], dig))
        val2 <- as.character(round(row.tab2[val.i], dig))
        val1c <- paste(prefix1, "\\SI{", gsub(".", dec.sep, val1, fixed = T), "}{", unit1, "}", suffix1, sep = "")
        val2c <- paste(prefix2, "\\SI{", gsub(".", dec.sep, val2, fixed = T), "}{", unit2, "}", suffix2, sep = "")
        both.vals <- paste(val1c, val2c)
        vals <- c(vals, both.vals)
        }
      cat(vals, sep = " & ")
      cat(eor)
      cat("\n")
      }
    }
  else {
    for (row.i in 1:nrow(tab)) {
      row.tab1 <- tab[row.i,]
      vals <- c()
        for (val.i in 1:length(row.tab1)) {
        val1 <- as.character(round(row.tab1[val.i], dig))
        val1c <- paste(prefix1, "\\SI{", gsub(".", dec.sep, val1, fixed = T), "}{", unit1, "}", suffix1, sep = "")
        vals <- c(vals, val1c)
        }
      cat(vals, sep = " & ")
      cat(eor)
      cat("\n")
      }
    }
}







0

Add a comment

Hi all, this is just an announcement.

I am moving Rcrastinate to a blogdown-based solution and am therefore leaving blogger.com. If you're interested in the new setup and how you could do the same yourself, please check out the all shiny and new Rcrastinate over at

http://rcrastinate.rbind.io/

In my first post over there, I am giving a short summary on how I started the whole thing. I hope that the new Rcrastinate is also integrated into R-bloggers soon.

Thanks for being here, see you over there.

Alright, seems like this is developing into a blog where I am increasingly investigating my own music listening habits.

Recently, I've come across the analyzelastfm package by Sebastian Wolf. I used it to download my complete listening history from Last.FM for the last ten years. That's a complete dataset from 2009 to 2018 with exactly 65,356 "scrobbles" (which is the word Last.FM uses to describe one instance of a playback of a song).
3

Giddy up, giddy it up

Wanna move into a fool's gold room

With my pulse on the animal jewels

Of the rules that you choose to use to get loose

With the luminous moves

Bored of these limits, let me get, let me get it like

Wow!

When it comes to surreal lyrics and videos, I'm always thinking of Beck. Above, I cited the beginning of the song "Wow" from his latest album "Colors" which has received rather mixed reviews. In this post, I want to show you what I have done with Spotify's API.

Click here for the interactive visualization

If you're interested in the visualisation of networks or graphs, you might've heard of the great package "visNetwork". I think it's a really great package and I love playing around with it. The scenarios of graph-based analyses are many and diverse: whenever you can describe your data in terms of "outgoing" and "receiving" entities, a graph-based analysis and/or visualisation is possible.
12

Here is some updated R code from my previous post. It doesn't throw any warnings when importing tracks with and without heart rate information. Also, it is easier to distinguish types of tracks now (e.g., when you want to plot runs and rides separately). Another thing I changed: You get very basic information on the track when you click on it (currently the name of the track and the total length).

Have fun and leave a comment if you have any questions.
3

So, Strava's heatmap made quite a stir the last few weeks. I decided to give it a try myself. I wanted to create some kind of "personal heatmap" of my runs, using Strava's API. Also, combining the data with Leaflet maps allows us to make use of the beautiful map tiles supported by Leaflet and to zoom and move the maps around - with the runs on it, of course.

So, let's get started. First, you will need an access token for Strava's API.

I've been using the ggplot2 package a lot recently. When creating a legend or tick marks on the axes, ggplot2 uses the levels of a character or factor vector. Most of the time, I am working with coded variables that use some abbreviation of the "true" meaning (e.g. "f" for female and "m" for male or single characters for some single character for a location: "S" for Stuttgart and "M" for Mannheim).

In my plots, I don't want these codes but the full name of the level.

It's been a while since I had the opportunity to post something on music. Let's get back to that.

I got my hands on some song lyrics by a range of artists. (I have an R script to download all lyrics for a given artist from a lyrics website.
4

Lately, I got the chance to play around with Shiny and Leaflet a lot - and it is really fun! So I decided to catch up on an old post of mine and build a Shiny application where you can upload your own GPX files and plot them directly in the browser.

Of course, you will need some GPX file to try it out. You can get an example file here (you gonna need to save it in a .gpx file with a text editor, though). Also, the Shiny application will always plot the first track saved in a GPX file.
9
Blog Archive
BlogRoll
BlogRoll
  • Announcing rixpress As I’ve already discussed in this vignette of my {rix} package, it is very easy to run a {targets} pipeline inside of a Nix environment for increased re...

    1 day ago
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.