Skip to content
Prev 366253 / 398502 Next

Is there a funct to sum differences?

Assuming John's understanding is correct, you can also do this without for 
loops. It takes getting used to vector and matrix arithmetic, which you
can read more about in the Introduction to R document that comes with R, 
or on R Exercises website [1].

You indicated having a problem with my last reproducible example... it did 
work, if you went through it one step at a time. If you skipped steps, you 
would have problems like you encountered. For completeness, I will give 
the whole reproducible example again here... don't mix in your own steps 
until you have worked through all the steps in this example... or at least 
if you do, go back and step through these steps one at a time if you 
change something that breaks it.

[1] http://r-exercises.com/2015/11/28/matrix-exercises/

#########------ begin
rates <- read.table( text =
"Date          Int
Jan-1959        5
Feb-1959        5
Mar-1959        5
Apr-1959        5
May-1959        5
Jun-1959        5
Jul-1959        5
Aug-1959        5
Sep-1959        5
Oct-1959        5
Nov-1959        5
", header = TRUE, colClasses = c( "character", "numeric" ) )

rates$thisone <- c(diff(rates$Int), NA)
rates$nextone <- c(diff(rates$Int, lag=2), NA, NA)
rates$lastone <- (rates$thisone + rates$nextone)/6.5*1000

rates$experiment1 <- rates$Int + c( rates$Int[ -1 ], NA )
rates$Int2 <- (1:11)^2
rates$experiment2 <- rates$Int2 + c( rates$Int2[ -1 ], NA )

# lag
N <- 5
# see ?embed, or https://en.wikipedia.org/wiki/Embedding
embed( c( rates$Int2, rep( NA, N ) ), N+1 )
# make a matrix of the same size as the embed result
matrix( rep( rates$Int2, N+1 ), ncol=N+1 )
# subtract the first values
embed( c( rates$Int2, rep( NA, N ) ), N+1 ) - rates$Int2
# or can rely on automatic replication ... depends on the
# fact that the embed result is a matrix which is really just
# a vector displayed in folded up form
embed( c( rates$Int2, rep( NA, N ) ), N+1 ) - rates$Int2
# anyway, the result can be computed in one line (wrapped for readability)
rates$experiment3 <- rowSums(   embed( c( rates$Int2
                                         , rep( NA, N )
                                         )
                                      , N+1
                                      )
                               - rates$Int2
                             , na.rm=TRUE
                             )
Date Int thisone nextone lastone experiment1 Int2 experiment2 experiment3
1  Jan-1959   5       0       0       0          10    1           5          85
2  Feb-1959   5       0       0       0          10    4          13         115
3  Mar-1959   5       0       0       0          10    9          25         145
4  Apr-1959   5       0       0       0          10   16          41         175
5  May-1959   5       0       0       0          10   25          61         205
6  Jun-1959   5       0       0       0          10   36          85         235
7  Jul-1959   5       0       0       0          10   49         113         170
8  Aug-1959   5       0       0       0          10   64         145         110
9  Sep-1959   5       0       0       0          10   81         181          59
10 Oct-1959   5       0      NA      NA          10  100         221          21
11 Nov-1959   5      NA      NA      NA          NA  121          NA           0

#dput(rates)
result <- structure(list(Date = c("Jan-1959", "Feb-1959", "Mar-1959", 
"Apr-1959", "May-1959", "Jun-1959", "Jul-1959", "Aug-1959", "Sep-1959", 
"Oct-1959", "Nov-1959"), Int = c(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5), thisone 
= c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA), nextone = c(0, 0, 0, 0, 0, 0,
0, 0, 0, NA, NA), lastone = c(0, 0, 0, 0, 0, 0, 0, 0, 0, NA,
NA), experiment1 = c(10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
NA), Int2 = c(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121), experiment2 = 
c(5, 13, 25, 41, 61, 85, 113, 145, 181, 221, NA), experiment3 = c(85,
115, 145, 175, 205, 235, 170, 110, 59, 21, 0)), .Names = c("Date",
"Int", "thisone", "nextone", "lastone", "experiment1", "Int2",
"experiment2", "experiment3"), row.names = c(NA, -11L), class = 
"data.frame")

#########------ end
On Sat, 24 Dec 2016, Fox, John wrote:

            
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k