Right, I had missed that one.

To the op: what Michael is saying can be shown with the following example.

x <- array(1:24, c(2,3,4))

r1 <- apply(x, -1, sum)  # remove dim 1
r2 <- apply(x, 2:3, sum) # include dims 2 and 3

all.equal(r1, r2)  # FALSE, different attributes
all(r1 == r2)  # TRUE, equal values

A negative index removes that removes that value from a vector, or row/column from a matrix or data.frame, or list element, etc. The index must be a number, for instance, data.frame column NAMES can't be used.
c(1, 2, 3)[-2] is c(1, 3).

So, the two forms above should be equivalent, but they're not. The first doesn't keep the dim attribute.

Rui Barradas

Em 23-06-2012 00:00, R. Michael Weylandt escreveu:
On Fri, Jun 22, 2012 at 5:53 PM, Rui Barradas<ruipbarra...@sapo.pt>  wrote:
Hello,

2L is an integer, 2 might be or not. In the case of apply(), there is no
difference.

All possible values for margin? Any possible(*) combination of the
dimensions of 'x' in

apply(x, margin, function)

(*) non-null. If, say, x<- array(0, dim=c(2,3,4)) (3dim) then you can call
any of

apply(x, 1, fun) or apply(x, 2, fun) or apply(x, 3, fun) but not margin=4;
apply(x, 1:2, fun) or apply(x, c(1, 3), fun) or ...
apply(x, 1:3, fun)
You can also get away with negative indexing in apply():

x<- array(1:24, dim = c(2,3,4))

apply(x, -1, sum)
apply(x, -c(1,3), sum)

but you seem to loose the array structure.

As usual, you can't mix

apply(x, c(-1, 2), sum) # ERROR

It also seems zero doesn't work here:

apply(x, 0, sum) # ERROR

Best,
Michael

Hope this helps,

Rui Barradas

Em 22-06-2012 23:23, Luba G escreveu:

What is the difference of using 2L versus 2 as the margin argument in the
apply() function? Where can I find detailed information on all of the
possible margin arguments?

x
      [,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    5    6
[4,]    7    8
[5,]    9   10
sqrt(apply(x, *2L*, function(r.st) var(r.st[!is.na(r.st)])))
[1] 3.162278 3.162278
sqrt(apply(x,* 2*, function(r.st) var(r.st[!is.na(r.st)])))
[1] 3.162278 3.162278

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to