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 annoyances
8 messages · Chalasani, Prasad, Uwe Ligges, Rod Montgomery +3 more
On Thu, 19 May 2005, Chalasani, Prasad wrote:
(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]".
This one is actually a FAQ,
mtx[,1,drop=FALSE]
-thomas
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 exp(mtx)
(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
Thomas Lumley wrote:
On Thu, 19 May 2005, Chalasani, Prasad wrote:
(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]".
This one is actually a FAQ,
mtx[,1,drop=FALSE]
-thomas
I wonder whether there is, or should be, a way to set FALSE as the default?
Rod Montgomery wrote:
Thomas Lumley wrote:
On Thu, 19 May 2005, Chalasani, Prasad wrote:
(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]".
This one is actually a FAQ,
mtx[,1,drop=FALSE]
-thomas
I wonder whether there is, or should be, a way to set FALSE as the default?
First question: No. Second question: No, because *many* functions do rely on the fact that x[,1] returns a vector rather than a matrix. 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
On Thu, 19 May 2005, Rod Montgomery wrote:
Thomas Lumley wrote:
This one is actually a FAQ,
mtx[,1,drop=FALSE]
-thomas
I wonder whether there is, or should be, a way to set FALSE as the default?
There shouldn't be (apart from editing the code), because you really don't want something this basic to be unpredictable. There have been discussions at several times about whether drop=FALSE or drop=TRUE should be the default. The decision has always been that programmers can cope either way, but that users probably don't expect mtx[,1] to be a vector, and that they definitely don't expect mtx[1,1] to be a matrix. -thomas
(a) There is 'stable.apply' in S Poetry that looks to me like it should work in R, but I haven't tested it. Patrick Burns Burns Statistics patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User")
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)) (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 PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
"TL" == Thomas Lumley <tlumley at u.washington.edu>
on Thu, 19 May 2005 09:39:13 -0700 (PDT) writes:
TL> On Thu, 19 May 2005, Rod Montgomery wrote:
>> Thomas Lumley wrote:
>>> This one is actually a FAQ, mtx[,1,drop=FALSE]
>>>
>>> -thomas
>>>
>> I wonder whether there is, or should be, a way to set
>> FALSE as the default?
TL> There shouldn't be (apart from editing the code),
TL> because you really don't want something this basic to be
TL> unpredictable.
TL> There have been discussions at several times about
TL> whether drop=FALSE or drop=TRUE should be the
TL> default. The decision has always been that programmers
TL> can cope either way, but that users probably don't
TL> expect mtx[,1] to be a vector, and that they definitely
TL> don't expect mtx[1,1] to be a matrix.
Yes, and (as Uwe has already mentioned),
the S language has now been ``defined'' in a few ways for
many years, and the decision to make "drop=TRUE" the default
(for arrays at least) may have been sub-optimal --- and maybe
could have been changed 15 years ago, but not anymore nowadays:
It is implicitly made use of in too many places of existing S code.
Those of you who are new to R:
Please don't assume R is new just because you are new to it!
Martin