# [R] R annoyances

Wiener, Matthew matthew_wiener at merck.com
Thu May 19 16:49:12 CEST 2005

```(a) If what you're trying to do is just apply exp, or any other element-wise
function, you can just say "exp(mtx)".  You avoid both "apply" and the
transpose, and save time in the bargain.  If your actual function really
does depend on multiple elements, it may be a little more complicated.  You
could conceivably write a "myapply" function to do the apply followed by the
transpose, but then of course you still need to keep track of which way
you're going.

(b) You want to look into the "drop = FALSE" option:
Sub.mtx <- mtx[,1,drop = FALSE]

Hope this helps,

Matt Wiener

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Chalasani, Prasad
Sent: Thursday, May 19, 2005 10:37 AM
To: r-help at stat.math.ethz.ch
Subject: [R] R annoyances

Dear R Folks,
I'm a big fan of R, but there are a couple of things
that repeatedly annoy me, and I wondered if anyone
has neat ways to deal with them.

(a) When using "apply" row-wise to a matrix, it returns
the results column-wise, and to preserve the original
orientation, I've to do a transpose. E.g. I've to keep
doing a transpose, which I consider to be quite annoying.

transformed.mtx <- t(apply( mtx, 1, exp))

(b) When extracting 2 or more columns of a matrix,
R returns the result as a matrix, BUT when extracting
just one column, it returns a vector/array, rather than
a matrix, so I've to keep doing as.matrix, which is annoying.

sub.mtx <- as.matrix(mtx[,1])

Of course I could write a suitable function
cols <- function(mtx,range) as.matrix(mtx[, range])
but then I lose the syntactic sugar of being able to say "[,1]".

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help