From: Suharto Anggono Suharto Anggono <suharto_anggono at yahoo.com>
Subject: Suggestions for 'diff.default'
To: R-devel at lists.R-project.org
Date: Monday, 28 January, 2013, 5:31 PM
I have suggestions for function
'diff.default' in R.
Suggestion 1: If the input is matrix, always return matrix,
even if empty.
What happens in R 2.15.2:
? ???[,1] [,2]
[1,]? ? 1? ? 2
diff(rbind(1:2))???# not matrix
R version 2.15.2 (2012-10-26)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats? ???graphics? grDevices
utils? ???datasets?
methods???base
The documentation for 'diff' says, "If 'x' is a matrix then
the difference operations are carried out on each column
separately."
If the result is empty, I expect that the result still has
as many columns as the input.
Suggestion 2: Make 'diff.default' applicable more generally
by
(a) not performing 'unclass';
(b) generalizing (changing)
ismat <- is.matrix(x)
to become
ismat <- length(dim(x)) == 2L
If suggestion 1 is to be applied, if 'unclass' is not wanted
(point (a) in suggestion 2 is also to be applied),
? ? if (lag * differences >= xlen)
??? return(x[0L])
can be changed to
? ? if (lag * differences >= xlen)
??? return(
? ? ? ? ? ? if (ismat) x[0L, ,
drop = FALSE] - x[0L, , drop = FALSE] else
? ? ? ? ? ? x[0L] - x[0L])
It will handle class where subtraction (minus) operation
change class.