[Rd] aggregate() naming -- bug or feature

2018-03-24 Thread lmo via R-devel
Be aware that the object that aggregate returns with bar() is more complicated 
than you think.
str(aggregate(iris$Sepal.Length, by = list(iris$Species), FUN = bar))
'data.frame':    3 obs. of  2 variables:
 $ Group.1: Factor w/ 3 levels "setosa","versicolor",..: 1 2 3
 $ x  : num [1:3, 1:2] 5.006 5.936 6.588 0.352 0.516 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr  "mean" "sd"
So you get a two column data.frame whose second column is a matrix.

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] substitute() on arguments in ellipsis ("dot dot dot")?

2018-08-15 Thread lmo via R-devel
A potential solution, at least in terms of producing the desired output, is the 
base R function alist:
> alist(1+2, "a", rnorm(3))
[[1]]
1 + 2

[[2]]
[1] "a"

[[3]]
rnorm(3)

> str(alist(1+2, "a", rnorm(3)))
List of 3
 $ : language 1 + 2
 $ : chr "a"
 $ : language rnorm(3)


luke

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] A different error in sample()

2018-09-20 Thread lmo via R-devel
Although it seems to be pretty weird to enter a numeric vector of length one 
that is not an integer as the first argument to sample(), the results do not 
seem to match what is documented in the manual. In addition, the results below 
do not support the use of round rather than truncate in the documentation. 
Consider the code below.
The first sentence in the details section says: "If x has length 1, is numeric 
(in the sense of is.numeric) and x >= 1, sampling via sample takes place from 
1:x."
In the console:> 1:2.001
[1] 1 2
> 1:2.9
[1] 1 2

truncation:
> trunc(2.9)
[1] 2

So, this seems to support the quote from in previous emails: "Non-integer 
positive numerical values of n or x will be truncated to the next smallest 
integer, which has to be no larger than .Machine$integer.max."
However, again in the console:> set.seed(123)
 > table(sample(2.001, 1, replace=TRUE))

   1    2    3 
5052 4941    7

So, neither rounding nor truncation is occurring. Next, define a sequence.
> x <- seq(2.001, 2.51, length.out=20)
Now, grab all of the threes from sample()-ing this sequence.

 > set.seed(123)
> threes <- sapply(x, function(y) table(sample(y, 1, replace=TRUE))[3])

Check for NAs (I cheated here and found a nice seed).> any(is.na(threes))
[1] FALSE
Now, the (to me) disturbing result.

> is.unsorted(threes)
[1] FALSE

or equivalently

> all(diff(threes) > 0)
[1] TRUE

So the number of threes grows monotonically as 2.001 moves to 2.5. As I hinted 
above, the monotonic growth is not assured. My guess is that the growth is 
stochastic and relates to some "probability weighting" based on how close the 
element of x is to 3. Perhaps this has been brought up before, but it seems 
relevant to the current discussion.
A potential aid to this issue would be something like
if(length(x) == 1 && !all.equal(x, as.integer(x))) warning("It is a bad idea to 
use vectors of length 1 in the x argument that are not integers.")
Hope that helps,luke

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel