Skip to content

Behavior of seq_along (was: Create a new var reflecting the order of subjects in existing var)

3 messages · Gabor Grothendieck, Benilton Carvalho

#
I am moving this from r-help to r-devel.  Based on offline communications
with Jim, suppose dat is defined as follows:

set.seed(123)
dat <- data.frame(ID= c(rep(1,2),rep(2,3), rep(3,3), rep(4,4),
   rep(5,5)), var1 =rnorm(17, 35,2), var2=runif(17,0,1))

# Then this ave call works as expected:

ave(dat$ID, dat$ID, FUN = function(x) seq_along(x))

# but this apparently identical calculation gives an error:

ave(dat$ID, dat$ID, FUN = seq_along)

The only difference between the two calls is that the first one
uses seq_along and the second uses function(x) seq_along(x)
in its place.

Does anyone know why the second gives an error?  Is this
a bug in the implementation of seq_along?
#
I'm not sure what your sessionInfo() is, but take a look at what I got:

 > set.seed(123)
 > dat <- data.frame(ID= c(rep(1,2),rep(2,3), rep(3,3), rep(4,4),
+    rep(5,5)), var1 =rnorm(17, 35,2), var2=runif(17,0,1))
 > dat
    ID     var1       var2
1   1 33.87905 0.02461368
2   1 34.53965 0.47779597
3   2 38.11742 0.75845954
4   2 35.14102 0.21640794
5   2 35.25858 0.31818101
6   3 38.43013 0.23162579
7   3 35.92183 0.14280002
8   3 32.46988 0.41454634
9   4 33.62629 0.41372433
10  4 34.10868 0.36884545
11  4 37.44816 0.15244475
12  4 35.71963 0.13880606
13  5 35.80154 0.23303410
14  5 35.22137 0.46596245
15  5 33.88832 0.26597264
16  5 38.57383 0.85782772
17  5 35.99570 0.04583117
 > ave(dat$ID, dat$ID, FUN = function(x) seq_along(x))
[1] 1 2 1 2 3 1 2 3 1 2 3 4 1 2 3 4 5
 > ave(dat$ID, dat$ID, FUN = seq_along)
[1] 1 2 1 2 3 1 2 3 1 2 3 4 1 2 3 4 5
 > sessionInfo()
R version 2.5.0 Under development (unstable) (2007-01-07 r40398)
x86_64-unknown-linux-gnu

locale:
LC_CTYPE=en_US.iso885915;LC_NUMERIC=C;LC_TIME=en_US.iso885915;LC_COLLATE 
=en_US.iso885915;LC_MONETARY=en_US.iso885915;LC_MESSAGES=en_US.iso885915 
;LC_PAPER=en_US.iso885915;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASU 
REMENT=en_US.iso885915;LC_IDENTIFICATION=C

attached base packages:
[1] "stats"     "graphics"  "grDevices" "utils"     "datasets"   
"methods"
[7] "base"
 >

cheers,
b
On Apr 3, 2007, at 12:40 AM, Gabor Grothendieck wrote:

            
#
Good point.  I tried it on

"R version 2.5.0 Under development (unstable) (2007-02-25 r40804)"

under Windows XP and both the seq_along and function(x) seq_along(x)
versions worked without error, as well.

So it seems this was a bug in 2.4.1 that is fixed in 2.5.0 .
On 4/3/07, Benilton Carvalho <bcarvalh at jhsph.edu> wrote: