[R] diff in a dataframe

2008-01-09 Thread Vishal Belsare
I have a dataframe say: date price_g price_s 0.340.56 0.360.76 . . . . . . and so on. say, 1000 rows. Is it possible to add two columns to this dataframe, by computing say diff(log(price

Re: [R] diff in a dataframe

2008-01-10 Thread Henrique Dallazuanna
Perhaps you can do this: cbind(df, sapply(rbind(c(NA, NA),log(df)), diff)) On 10/01/2008, Vishal Belsare <[EMAIL PROTECTED]> wrote: > I have a dataframe say: > > date price_g price_s > 0.340.56 > 0.360.76 >. . >.

Re: [R] diff in a dataframe

2008-01-10 Thread bartjoosen
Maybe: cbind(df,rbind(NA,apply(log(df),2,diff))) Bart Vishal Belsare wrote: > > I have a dataframe say: > > date price_g price_s > 0.340.56 > 0.360.76 >. . >. . >. . > > and so

Re: [R] diff in a dataframe

2008-01-10 Thread Don MacQueen
So, what's the easiest way to add a column to a dataframe? Just do it. Here is a really simple example to illustrate: > foo <- data.frame(x=1:4,y=2:5) > foo x y 1 1 2 2 2 3 3 3 4 4 4 5 > foo$z <- c(NA,diff(foo$x)) > foo x y z 1 1 2 NA 2 2 3 1 3 3 4 1 4 4 5 1 Solutions using apply

Re: [R] diff in a dataframe

2008-01-10 Thread anna freni sterrantino
1.021651 -0.2744368 3 3.00 4.00 1.098612 1.3862944 and if you wnat to coerce the first row to a whatever value just type a[1,]<-NA Cheers A - Messaggio originale - Da: Vishal Belsare <[EMAIL PROTECTED]> A: r-help@r-project.org Inviato: Mercoledì 9 gennaio 2008, 23:16:38 Og

Re: [R] diff in a dataframe

2008-01-10 Thread Gabor Grothendieck
Represent this as a time series. Using the zoo package: > library(zoo) > z <- zoo(cbind(price_g = c(0.34, 0.36), price_s = c(0.56, 0.76)), > as.Date(c("2000-01-01", "2000-01-05"))) > diff(log(z)) price_g price_s 2000-01-05 0.05715841 0.3053816 > diff(log(z), na.pad = TRUE)

Re: [R] diff in a dataframe

2008-01-10 Thread Vishal Belsare
Thanks Henrique, Don and Gabor. I did come around to solving it by using the zoo library. Very useful stuff that one for handling a bunch of long irregular time series. Gabor, thanks for your present and previous responses. The quickref was indeed helpful. I do have another question regarding zoo

Re: [R] diff in a dataframe

2008-01-10 Thread Gabor Grothendieck
This is consistent with how matrices and ts series in R work: they all use x[,j] only. On Jan 10, 2008 1:08 PM, Vishal Belsare <[EMAIL PROTECTED]> wrote: > Thanks Henrique, Don and Gabor. I did come around to solving it by > using the zoo library. Very useful stuff that one for handling a bunch >