[R] function ave() with seq_along returning char sequence instead of numeric

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Mon Oct 31 23:29:01 CET 2016


Given  the following R statements

v <-  c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'c')
ave(v, list(v), FUN=seq_along)
 [1] "1" "2" "3" "1" "2" "3" "1" "2" "3" "4" 

I was expecting to get a numeric vector back.  I apparently have missed something in the documentation.  If vector v is character, then the numeric sequence is converted to character before returning.  I can work around by doing something like

ave(seq_along(v), list(v), FUN=seq_along)
 [1] 1 2 3 1 2 3 1 2 3 4 

Is the work around the best way to go, or have I missed an option in the documentation?


Thanks,

Dan

Daniel Nordlund, PhD
Research and Data Analysis Division
Services & Enterprise Support Administration
Washington State Department of Social and Health Services



More information about the R-help mailing list