Skip to navigation

Small hyperref error solved

November 9th, 2009

For the record. I got an error running hyperref:

Paragraph ended before \Hy@setref@link was complete

This was due to having a regulare closing parenthesis directly after the curly bracket that closed a \ref, like this: (see section \ref{section-1}). Putting in a space between the closing curly bracket and parenthesis solved the problem: ... section-1} )

Nice R example from r-help

November 5th, 2009

I don’t know the first thing about programming, but sometimes you see something and appreciate how elegant it is. This small solution from r-help is a good example. Someone wanted, given a vector c('p','p','t','t','t','p','k','t') to produce NA NA 1 2 3

test <- c('p','p','t','t','t','p','k','t')
v <- cumsum(ind <- test == 't')
v[!ind] <- NA

I didn't know you could assign something to an object and use it at the same time. Very neat. So if you wanted this all at once, you could also do:

v <- ifelse(cumsum(ind <- test=='t')==0,NA,cumsum(ind))


ifelse((v <- cumsum(test=='t'))==0,NA,v)

Maybe Dimitris' solution is in fact preferred for R, using indexing instead of if/ else, but I don't know enough about R to know whether that makes a difference.

Mutt: set dynamic email signature from shell command output

October 3rd, 2009

I want to set my signature when mailing to the r-help mailing list to contain information about the R version I have currently installed. Creating a bash script that directly echoes the several lines that I want to have in my signature and executing that in the muttrc using backticks gives a “broken pipe” error.

One solution (I’m sure there are others, maybe better ones) is to make the shell script create a temporary signature file and echo the path to that file, then execute the shell script using backticks.

Shell script (don’t forget to chmod 744):



echo  "Marianne Promberger PhD, King's College London                                       " > $thesigfile
R --version | head -n 1 >> $thesigfile
cat /etc/issue | head -n 1 | cut -d " " -f 1-2 >> $thesigfile
echo $thesigfile


send-hook '~t r-help' 'set signature=`~/.mutt/signature_r-help`' needs MIME-tools perl module

October 3rd, 2009 fails with:

Can't locate MIME/ in @INC


sudo aptitude install libmime-tools-perl

R: watch out when comparing vectors using “==”

September 25th, 2009

This question was on the R-help mailing list today:

I have a data frame “test”:


I have a vector "needed":


I need the result to look like this:

1 2 7
2 3 7
5 6 9
6 7 9

When I do the following:


I only get unique rows that have 7 or 9 in "total":

1 2 7
6 7 9


The solution is to use %in% instead:

test[test$total %in% needed,]

A quick aside: note that you don't select the column "total" by using test["total"], instead, you need to index correctly and use test[,"total"], or, more succinctly, use test$total for dataframes. Check also identical(test["total"],test$total) and identical(test[,"total"],test$total).

Why doesn't == work here? It works just fine if you want to find all rows where the column total equals 7:

test[test$total == 7,]

The trick is that to R, test$total, needed and 7 are all vectors. == compares them element by element. Since 7 and needed are shorter than test$total, they are recycled as often as needed to give the same length as test$total. (Note, by the way, that you can of course directly look at the logical index by typing: test$total==needed.) So what R is doing is comparing this:

> test$total
[1]  7  7  8  8  9  9 10 10

to this:

> rep(needed,length.out=length(test$total))
[1] 7 9 7 9 7 9 7 9

The two vectors just happen to coincide at test$total[1] and test$total[6]. The recycling is of course no problem when you look for matches with a single number, because:

> rep(7,length.out=length(test$total))
[1] 7 7 7 7 7 7 7 7

will do just fine.

To get the help text on %in% by the way, you need to quote it: