Skip to navigation

R: Logical operators: Beware of the difference between “&&” and “&”

September 12th, 2008

There’s a tricky bit about R’s logical operators, and though it’s described in the help pages that you get when you type, for example,

?"&"

I still fall from it from time to time. (I think it has to do with my rudimentary knowledge and usage of “&&” for if-statements in bash shell scripts.

Below I just go through “&&” and “&”. Obviously, this also applies to “||” and “|”.

Here’s a little demo to remind me and to demonstrate this to newbies:

> 1==1
[1] TRUE
> 1==1 & 1==2
[1] FALSE
> 1==1 && 1==2
[1] FALSE

So far, so good. Now look what happens if we apply this to vectors:

> 1:3==1:3
[1] TRUE TRUE TRUE
> 1:3==c(1,3,3)
[1]  TRUE FALSE  TRUE
> 1:3==1:3 & 1:3==c(1,3,3)
[1]  TRUE FALSE  TRUE

This is what you probably want in most cases: element-wise comparison. The “&” here compares each element in the vector “TRUE TRUE TRUE” to the corresponding element in “TRUE FALSE TRUE” and returns a “TRUE” each time they match and a “FALSE” if they don’t. Now look what happens if we use “&&”:

> 1:3==1:3 && 1:3==1:3
[1] TRUE
> 1:3==1:3 && 1:3==c(1,3,3)
[1] TRUE

For “&&”, as the R help page says, “The longer form evaluates left to right examining only the first element of each vector.”

This is quite obvious in this example, but it can get confusing when you use a logical operator to index a vector. Watch this:

> x <- 1:5
> x[x<5]
[1] 1 2 3 4
> x[x<5 & x>2]

Fine.

> x[x<5 && x>2]
integer(0)

The reason:

> x>2 & x<5
[1] FALSE FALSE  TRUE  TRUE FALSE
> x>2 && x<5
[1] FALSE

So in the first case, R “sees”

> x[c(FALSE,FALSE,TRUE,TRUE,FALSE)]

and in the second case, it sees:

> x[FALSE]

As long as the first element compares with “FALSE”, you’re actually lucky, because the error will be obvious. It’s tricky when “&&” evaluates to “TRUE” when it looks at the first element, because some R functions will recycle input if it is too short, and this can lead to things like:

> x <- 1:10
> y <- c(1,3:11)
> x==y
 [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> x[x==1 & y==1]
[1] 1
> x[x==1 && y==1]
 [1]  1  2  3  4  5  6  7  8  9 10

So basically, for logical comparison, stick to “&” unless you know you need “&&”.

By the way, if you want to see the values that are elements of both x an y, use “%in%”:

> x[x%in%y]
[1]  1  3  4  5  6  7  8  9 10
> y[y%in%x]
[1]  1  3  4  5  6  7  8  9 10

But don’t do:

> y[x%in%y]
[1]  1  4  5  6  7  8  9 10 11

This indexes y using the logical vector returned by “x%in%y”, which is of course:

> x%in%y
 [1]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE

Disable boot process checking for keyboard on Dell desktop

September 5th, 2008

Whenever I forgot to plug in my keyboard before booting my Dell desktop, it would halt with the amusing error message:

Keybard failure. Press F1 to continue, F2 for setup.

If I was at home, I could just plug in the keyboard and hit F1, but if I was trying to boot the computer remotely through wake-on-LAN, I was out of luck.

Turns out you can disable this in the BIOS (hit F2). On my Dell, it is under the “Standard CMOS features” section. You’ll find “Halt on …”, which was set to “All errors” for me. Switching this to “All but keyboard” disables the boot process checking for keyboard presence.

Installing okular in Gutsy

August 20th, 2008

I’m running Xubuntu but I don’t see why this wouldn’t work for Ubuntu.

I really like kpdf, but it cannot rotate pages. It seems that it is no longer actively developed, and that instead okular is kpdf’s successor. However, in Gutsy, if you try installing okular via aptitude, you get a dependency problem that aptitude cannot resovle for you (something about missing the virtual package libpoppler1).

You can work around this by adding the following repository to your /etc/apt/sources.list:

deb http://ppa.launchpad.net/kubuntu-members-kde4/ubuntu gutsy main

then:

sudo aptitude update && sudo aptitude dist-upgrade

This installs quite a few kde things and may reinstall kde apps you might already have installed, such as amarok. It did for me. My guess is the problem is that okular is a kde4 app, and you need some kde4 stuff for it. I haven’t checked whether this breaks any of the other kde apps on your system! (Personally, I don’t care — I do have amarok installed but I’m hardly using it; I’ve grown fond of quodlibet instead).

Then, you can do:

sudo aptitude install okular

This will install a lot of dependencies.

You won’t be able to start okular by just using the command okular in a terminal, but you can find it in the Xfce menu under the “Graphics” applications. However, a

ps -ef | grep okular

while okular is running reveals that it sits at /usr/lib/kde4/bin/okular, so you can add

alias okular="/usr/lib/kde4/bin/okular"

to your ~/.bashrc (or, maybe add /usr/lib/kde4/bin to your PATH).

MikTeX package manager for Linux

June 15th, 2008

One of the few things I really missed when I switched from Windows to Linux was MikTeX, since it makes installation of LaTeX packages very easy, where Linux makes you jump through hoops. So I’m delighted that MikTeX brings this functionality to Linux, trough MikTeX Tools.

Since this is not available through aptitude, you have to download the source and compile, it but it’s straightforward if you follow the readme file that comes with the package. Above all, Ubuntu proved great as usual at helping me get the missing dependencies, by automatically suggesting which package I needed to aptitude when I typed a command it didn’t find (curl-config and cmake).

I just had one problem: when I tried to update the mpm database, I got:

mpm --update-db
mpm: error while loading shared libraries: libMiKTeX207-core.so.1: cannot open shared object file: No such file or directory

It turns out the source files were in a different location, and all I had to do was:

sudo ldconfig

Now installing packages is as easy as:

mpm --install=emerald

Binding a key to the middle mouse button with xmodmap and xkbset

June 3rd, 2008

Great, I just found this here. This is how you can bind a key to the middle mouse button (I use the middle mouse button a lot to (a) paste stuff from the clipboard, (b) open links in background tabs in Firefox). I used the pause/break key, so it’s slightly modified. I first had to get xkbset via aptitude.

sudo aptitude install xkbset
xmodmap -e 'keysym Pause Break = Pointer_Button2'
xkbset m
xkbset exp =m

I don’t know yet whether this will persist after a reboot.