Skip to content
Prev 366247 / 398506 Next

Is there a funct to sum differences?

You need to "reply all" so other people can help as well, and others can 
learn from your questions.

I am still puzzled by how you expect to compute "finalone". If you had 
supplied numbers other than all 5's it might have been easier to figure 
out what is going on.

What is your purpose in performing this calculation?

#### reproducible code
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" ) )

#your code
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
# I doubt there is a ready-built function that knows you want to
# divide by 6.5 or multiply by 1000

# form a vector from positions 2:11 and append NA)
rates$experiment1 <- rates$Int + c( rates$Int[ -1 ], NA )
# numbers that are not all the same
rates$Int2 <- (1:11)^2
rates$experiment2 <- rates$Int2 + c( rates$Int2[ -1 ], NA )

# 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), Int2 = c(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121), experiment1 = 
c(10,
10, 10, 10, 10, 10, 10, 10, 10, 10, NA), experiment2 = c(5, 13,
25, 41, 61, 85, 113, 145, 181, 221, NA)), .Names = c("Date",
"Int", "thisone", "nextone", "lastone", "Int2", "experiment1",
"experiment2"), row.names = c(NA, -11L), class = "data.frame")
On Sat, 24 Dec 2016, arthur brogard 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