Jeff Newmiller
2016-Oct-31 23:34 UTC
[R] function ave() with seq_along returning char sequence instead of numeric
The help page describes the first argument x as a numeric... it is not designed to accept character, so the fact that you get anything even close to right is just a bonus. As the doctor says, "if it hurts, don't do that". ave( rep( 1, length( v ), v, FUN=seq_along ) -- Sent from my phone. Please excuse my brevity. On October 31, 2016 3:29:01 PM PDT, "Nordlund, Dan (DSHS/RDA)" <NordlDJ at dshs.wa.gov> wrote:>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 > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.
Nordlund, Dan (DSHS/RDA)
2016-Nov-01 00:11 UTC
[R] function ave() with seq_along returning char sequence instead of numeric
Jeff, Thanks for the response. You are right of course. I went back and reread the help page. I guess I just glossed over the 'x is numeric' statement because the default FUN was the mean, and seq_along doesn't care about type. I should know better than to disregard what I read in the help pages. But you are right, using any numeric vector of appropriate size as the first argument does what I need. Dan Daniel Nordlund, PhD Research and Data Analysis Division Services & Enterprise Support Administration Washington State Department of Social and Health Services> -----Original Message----- > From: Jeff Newmiller [mailto:jdnewmil at dcn.davis.ca.us] > Sent: Monday, October 31, 2016 4:34 PM > To: Nordlund, Dan (DSHS/RDA); r-help at r-project.org > Subject: Re: [R] function ave() with seq_along returning char sequence > instead of numeric > > The help page describes the first argument x as a numeric... it is not > designed to accept character, so the fact that you get anything even close to > right is just a bonus. > > As the doctor says, "if it hurts, don't do that". > > ave( rep( 1, length( v ), v, FUN=seq_along ) > -- > Sent from my phone. Please excuse my brevity. > > On October 31, 2016 3:29:01 PM PDT, "Nordlund, Dan (DSHS/RDA)" > <NordlDJ at dshs.wa.gov> wrote: > >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 > > > >______________________________________________ > >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >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.
S Ellison
2016-Nov-01 15:29 UTC
[R] function ave() with seq_along returning char sequence instead of numeric
> The help page describes the first argument x as a numeric...It also describes the _value_ as numeric. One for the help page issue list? In fact there seems no obvious reason for a hard restriction to numeric*; the return value will depend largely on what FUN does, as there's no argument class check in the code for ave or for split(), which ave()uses. The principal requirement is presumably that FUN must accept a vector of class class(x) and return a vector of the same length as its argument (or, if there's grouping, a scalar) that split<- (or, with no grouping factor, '<-') can use. *though plenty of reason to warn of unexpected consequences if not, of course S Ellison ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
Charles C. Berry
2016-Nov-01 16:24 UTC
[R] function ave() with seq_along returning char sequence instead of numeric
On Mon, 31 Oct 2016, Jeff Newmiller wrote:> The help page describes the first argument x as a numeric... it is not > designed to accept character,Actually it is so designed, but not advertised as such. See below.> so the fact that you get anything even close to right is just a bonus. > > As the doctor says, "if it hurts, don't do that". > > ave( rep( 1, length( v ), v, FUN=seq_along ) > --[snip] Reading the code of `ave` and then `split<-.default`, you will see subset replacement, "x[i]<- ...", on the argument 'x'. So, the issue is having FUN and that replacement (and possible coercion) yield something useful/sensible. In other words, class(x) need not be "numeric". For instance, operating on "Date" objects:> # start at 2016-01-02, step 10 days, ... > x <- as.Date("2016-01-01")+seq(1,1000,by=10) > z <- rep(1:10, 10) > class(ave(x,z)) # Date class is preserved[1] "Date"> ave(x,z) # mean date[1] "2017-03-27" "2017-04-06" "2017-04-16" "2017-04-26" ...> ave(x,z,FUN=min) # earliest date[1] "2016-01-02" "2016-01-12" "2016-01-22" "2016-02-01" ... However, trying to describe this feature in the help page without a lot of detail and examples might confuse more users than it would enlighten. -- Chuck