[R] using diff function with by function

Rui Barradas ruipbarradas at sapo.pt
Mon Mar 10 20:54:34 CET 2014


Hello,

Maybe you want

by(tr$srv_dt, tr$Rmbr, diff, lag=1, difference=1)


Can't you ?dput a data example?

dput(head(tr, 30))  # paste the output of this in a post


Hope this helps,

Rui Barradas

Em 10-03-2014 16:10, Mckinstry, Craig escreveu:
> I have been trying to process a data.frame (tr) on a grouping factor (Rmbr) using diff
> to find differences between srv_dt (a date) by Rmbr and keep getting the following error message
>
>> tmp <- by(tr,tr$Rmbr, diff, tr$srv_dt, lag=1, difference=1)
> Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] :
>    non-numeric argument to binary operator
> # the only two arguments I supply are both numeric
>> mode(tr$srv_dt)
> [1] "numeric"
>> mode(tr$Rmbr)
> [1] "numeric"
>
>
> IMPORTANT NOTICE: This communication, including any attachment, contains information that may be confidential or privileged, and is intended solely for the entity or individual to whom it is addressed.  If you are not the intended recipient, you should delete this message and are hereby notified that any disclosure, copying, or distribution of this message is strictly prohibited.  Nothing in this email, including any attachment, is intended to be a legally binding signature.
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>




More information about the R-help mailing list