Hello, I created a data.frame which contains two columns: df$P (Power) et df$DateTime (time). I'd like to add a new column df$diffP (difference of Power between T and T-2). I made a loop : for (i in 3:length(df$DateTime)){ df$diffP[i] = df$P[i] - df$P[i-2] } execution time result is unaceptable: 24s !! Is there any way to reduce complexity about O(n) ? for example 2 or 3s (10s maxi) Does anybody find better than ~24s ? thanks for your help -- View this message in context: http://r.789695.n4.nabble.com/data-frame-temporal-complexity-tp4269585p4269585.html Sent from the R help mailing list archive at Nabble.com.
1. You are doing exactly what one is recommended to avoid in R. Have you read an Introduction to R? -- especially about "vectorization"? 2. To answer your question: ?diff It will probably be an order or 2 of magnitude faster. -- Bert On Fri, Jan 6, 2012 at 6:39 AM, ikuzar <razuki at hotmail.fr> wrote:> Hello, > > I created a data.frame which contains two columns: df$P (Power) et > df$DateTime (time). I'd like to add a new column df$diffP (difference of > Power between T and T-2). > > I made a loop : > > for (i in 3:length(df$DateTime)){ > ? ?df$diffP[i] = df$P[i] - df$P[i-2] > } > execution time result is unaceptable: 24s !! > > Is there any way to reduce complexity about O(n) ? for example 2 or 3s (10s > maxi) > > Does anybody find better than ~24s ? > > thanks for your help > > -- > View this message in context: http://r.789695.n4.nabble.com/data-frame-temporal-complexity-tp4269585p4269585.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Try this:> df <- data.frame(power = runif(10000)) > # add difference > diffGap <- 2 > df$diff <- c(rep(NA, diffGap), head(df$power, -diffGap) - tail(df$power, -diffGap)) > head(df, 10)power diff 1 0.86170585 NA 2 0.90672473 NA 3 0.96868367 -0.10697782 4 0.44199262 0.46473211 5 0.48593923 0.48274443 6 0.99409592 -0.55210330 7 0.72790728 -0.24196804 8 0.38070013 0.61339579 9 0.69913680 0.02877047 10 0.07902925 0.30167088>On Fri, Jan 6, 2012 at 9:39 AM, ikuzar <razuki at hotmail.fr> wrote:> Hello, > > I created a data.frame which contains two columns: df$P (Power) et > df$DateTime (time). I'd like to add a new column df$diffP (difference of > Power between T and T-2). > > I made a loop : > > for (i in 3:length(df$DateTime)){ > ? ?df$diffP[i] = df$P[i] - df$P[i-2] > } > execution time result is unaceptable: 24s !! > > Is there any way to reduce complexity about O(n) ? for example 2 or 3s (10s > maxi) > > Does anybody find better than ~24s ? > > thanks for your help > > -- > View this message in context: http://r.789695.n4.nabble.com/data-frame-temporal-complexity-tp4269585p4269585.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Ok, thanks, it works ! -- View this message in context: http://r.789695.n4.nabble.com/data-frame-temporal-complexity-tp4269585p4270073.html Sent from the R help mailing list archive at Nabble.com.
Something like this (not tested)? df$diffP <- c(NA, NA, diff(df$P,2)) -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 1/6/12 6:39 AM, "ikuzar" <razuki at hotmail.fr> wrote:>Hello, > >I created a data.frame which contains two columns: df$P (Power) et >df$DateTime (time). I'd like to add a new column df$diffP (difference of >Power between T and T-2). > >I made a loop : > >for (i in 3:length(df$DateTime)){ > df$diffP[i] = df$P[i] - df$P[i-2] >} >execution time result is unaceptable: 24s !! > >Is there any way to reduce complexity about O(n) ? for example 2 or 3s >(10s >maxi) > >Does anybody find better than ~24s ? > >thanks for your help > >-- >View this message in context: >http://r.789695.n4.nabble.com/data-frame-temporal-complexity-tp4269585p426 >9585.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >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.