[R] R annoyances

Uwe Ligges ligges at statistik.uni-dortmund.de
Thu May 19 16:49:16 CEST 2005

Chalasani, Prasad wrote:
> 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))

I'd rather type


> (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]".

The docs suggest:

   mtx[ , 1, drop = FALSE]

Uwe Ligges

> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list