[R] Caculate first difference from a dataframe; write a simulation

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Aug 5 12:57:07 CEST 2009


On Tue, 2009-08-04 at 23:42 +0530, Meenu Sahi wrote:
> Dear R Users
> 
> I'm writing my first simulation in R.
> I've put across my problems with a smaller example in the attachment along
> with the questions.

Q1 - mydat4[, 1:2] is not a matrix! It is a data.frame.

> class(mydat4[, 1:2])
[1] "data.frame"
> is.matrix(mydat4[, 1:2])
[1] FALSE

?diff is explicit about 'x' needing to be a numeric vector or a matrix.

Cast mydat4[, 1:2] as a matrix and it will work:

> diff(as.matrix(mydat4[,1:2]),1)
   Level spread
3   0.27   2.27
6   0.15   0.01
8   0.58  -2.15
9   0.48   1.43
81 -0.48  -1.43
91  0.48   1.43
2   0.17   0.26
10  0.95  -2.38
1  -0.83   1.94
11  0.32  -1.36

HTH

G

> 
> Please help.
> 
> Best regards
> Meenu
> ______________________________________________
> 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.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090805/183f4f74/attachment-0002.bin>


More information about the R-help mailing list