[R] Is there a funct to sum differences?
Jeff Newmiller
jdnewmil at dcn.davis.ca.us
Sat Dec 24 06:04:33 CET 2016
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:
>
>
> Yes, sure, thanks for your interest. I apologise for not submitting in the correct manner. I'll learn (I hope).
>
> Here's the source - a spreadsheet with just two columns, date and 'Int'.
>
>
> 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
>
>
> After processing it becomes this:
>
>
>> rates
> Date Int thisone nextone lastone finalone
> 1 1959-01-01 5.00 0.00 0.00 0.000000 10
> 2 1959-02-01 5.00 0.00 0.00 0.000000 10
> 3 1959-03-01 5.00 0.00 0.00 0.000000 10
> 4 1959-04-01 5.00 0.00 0.00 0.000000 10
> 5 1959-05-01 5.00 0.00 0.00 0.000000 10
> 6 1959-06-01 5.00 0.00 0.00 0.000000 10
>
> The one long column I'm referring to is the 'Int' column which R has imported.
>
> The actual code is:
>
>
> rates <- read.csv("Rates2.csv",header = TRUE,colClasses=c("character","numeric"))
>
> sapply(rates,class)
>
> rates$Date <- strptime(paste0("1-", rates$Date), format="%d-%b-%Y", tz="UTC")
>
>
> 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
>
>
>
> ab
>
>
>
> ----- Original Message -----
> From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
> To: arthur brogard <abrogard at yahoo.com>; arthur brogard via R-help <r-help at r-project.org>; "r-help at r-project.org" <r-help at r-project.org>
> Sent: Saturday, 24 December 2016, 13:25
> Subject: Re: [R] Is there a funct to sum differences?
>
> Could you make your example reproducible? That is, include some sample input and output. You talk about a column of numbers and then you seem to work with named lists and I can't reconcile your words with the code I see.
> --
> Sent from my phone. Please excuse my brevity.
>
>
> On December 23, 2016 3:40:18 PM PST, arthur brogard via R-help <r-help at r-project.org> wrote:
>> I've been looking but I can't find a function to sum difference.
>>
>> I have this code:
>>
>>
>> rates$thisone <- c(diff(rates$Int), NA)
>> rates$nextone <- c(diff(rates$Int, lag=2), NA, NA)
>> rates$lastone <- (rates$thisone + rates$nextone)
>>
>>
>> It is looking down one long column of numbers.
>>
>> It sums the difference between the first two and then between the first
>> and third and so on.
>>
>> Can it be made to automatically sum the difference between the first
>> and subsequent to the end of a list?
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
---------------------------------------------------------------------------
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
More information about the R-help
mailing list