Re: [R] Speeding up R code - Apply a function to each row of a matrix using the dplyr package

2018-11-01 Thread Jeff Newmiller
As Don suggests, looking for ways to do the whole calculation at once is a big efficiency booster. Also, avoiding unnecessary calculations (e.g. mean of 1:n is (n+1)/2 and mean(x+a) where a is a constant is mean(x)+a. Reproducible example: #library(tictoc) library(microben

Re: [R] Speeding up R code - Apply a function to each row of a matrix using the dplyr package

2018-11-01 Thread MacQueen, Don via R-help
Without more study, I can only give some general pointers. The as.vector() in X1 <- as.vector(coord[1]) is almost certainly not needed. It will add a little bit to your execution time. Converting the output of func() to a one row matrix is almost certainly not needed. Just return c(res1, res2).

[R] Speeding up R code - Apply a function to each row of a matrix using the dplyr package

2018-11-01 Thread Nelly Reduan
Hello, I have a input data frame with multiple rows. For each row, I want to apply a function. The input data frame has 1,000,000+ rows. How can I speed up my code ? I would like to keep the function "func". Here is a reproducible example with a simple function: library(tictoc) library

[R] Speeding up npreg

2018-02-03 Thread Michael Haenlein
Dear all, I am using npreg from the np library to run a Kernel regression. My dataset is relatively large and has about 3000 observations. The dependent variable is continuous and I have a total of six independent variables -- two continuous, two ordinal and two categorical. The model converges w

Re: [R] Speeding up code?

2015-07-16 Thread Ignacio Martinez
Thank Jim! This makes a huge difference. Can you explain why are data frame slower than a matrix? Any other suggestions on how to improve the code would be greatly appreciated. Thanks again! Ignacio On Thu, Jul 16, 2015 at 1:42 PM jim holtman wrote: > Actually looking at the result, you don't

Re: [R] Speeding up code?

2015-07-16 Thread jim holtman
Actually looking at the result, you don't need the transpose; that was an artifact of how you were doing it before. xm <- do.call(rbind, str_split(string = AllpairsTmp, pattern = "-")) # convert to dataframe and do transpose on matrix and not dataframe separoPairs <- as.data.frame((xm), strings

Re: [R] Speeding up code?

2015-07-16 Thread jim holtman
Here is one improvement. Avoid dataframes in some of these cases. This create a character matrix and then converts to a dataframe after doing the transpose of the matrix. This just takes less than 10 seconds on my system: > library(stringr) > # create character matrix; avoid dataframes in th

Re: [R] Speeding up code?

2015-07-16 Thread Ignacio Martinez
Hi Collin, The objective of the gen.names function is to generate N *unique *random names, where N is a *large *number. In my computer `gen.names(n = 5)` takes under a second, so is probably not the root problem in my code. That said, I would love to improve it. I'm not exactly sure how you pr

Re: [R] Speeding up code?

2015-07-15 Thread Collin Lynch
Hi Ignacio, If I am reading your code correctly then the top while loop is essentially seeking to select a random set of names from the original set, then using unique to reduce it down, you then iterate until you have built your quota. Ultimately this results in a very inefficient attempt at samp

[R] Speeding up code?

2015-07-15 Thread Ignacio Martinez
Hi R-Help! I'm hoping that some of you may give me some tips that could make my code more efficient. More precisely, I would like to make the answer to my stakoverflow question more efficient

Re: [R] speeding up applying hist() over rows of a matrix

2014-05-02 Thread Ortiz-Bobea, Ariel
This works great, thanks a lot! -AOB -Original Message- From: William Dunlap [mailto:wdun...@tibco.com] Sent: Friday, May 02, 2014 12:31 PM To: Ortiz-Bobea, Ariel Cc: r-help@r-project.org Subject: Re: [R] speeding up applying hist() over rows of a matrix And since as.integer(cut(x,bins

Re: [R] speeding up applying hist() over rows of a matrix

2014-05-02 Thread William Dunlap
And since as.integer(cut(x,bins)) is essentially findInterval(x,bins) (since we throw away the labels made by cut()), I tried using findInterval instead of cut() and it cut the time by more than half, so your 5.0 s. is now c. 0.1 s. f3 <- function (m, bins) { nbins <- length(bins) - 1L m <-

Re: [R] speeding up applying hist() over rows of a matrix

2014-05-02 Thread William Dunlap
Your original code, as a function of 'm' and 'bins' is f0 <- function (m, bins) { t(apply(m, 1, function(x) hist(x, breaks = bins, plot = FALSE)$counts)) } and the time it takes to run on your m1 is about 5 s. on my machine > system.time(r0 <- f0(m1,bins)) user system elapsed 4.950.0

[R] speeding up applying hist() over rows of a matrix

2014-05-01 Thread Ortiz-Bobea, Ariel
Hello everyone, I'm trying to construct bins for each row in a matrix. I'm using apply() in combination with hist() to do this. Performing this binning for a 10K-by-50 matrix takes about 5 seconds, but only 0.5 seconds for a 1K-by-500 matrix. This suggests the bottleneck is accessing rows in

Re: [R] Speeding up code

2013-11-25 Thread MacQueen, Don
ditto to everything Jeff Newmiller said, but I'll take it a little further. I'm guessing that with df <- data.frame(31790,31790) you thought you were creating something with 31790 rows and 31790 columns. You weren't. You were creating a data frame with one row and two columns: > data.frame(317

Re: [R] Speeding up code

2013-11-23 Thread Jeff Newmiller
What is cldm? We (and therefore you, to verify that we can) should be able to copy the example from the email and paste it into a newly-started instance of R. Not having some example data similar to yours to work with puts us at a major disadvantage. It would also be helpful to know what you ar

[R] Speeding up code

2013-11-23 Thread Amie Hunter
Hello R experts, I'm new to R and I'm wanting to know what is the best way to speed up my code. I've read that you can vectorize the code but I'm unsure on how to implement this into my code. df <- data.frame(31790,31790) for (i in 1:31790) {   for (j in i:31790)   {     ken<-cor(cldm[i,3:

Re: [R] speeding up "sum of squared differences" calculation

2013-10-22 Thread William Dunlap
hem. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of S Ellison > Sent: Tuesday, October 22, 2013 6:08 AM > To: r-help@r-project.org > Cc: Ken Kn

Re: [R] speeding up "sum of squared differences" calculation

2013-10-22 Thread Hadley Wickham
> There's little practical difference; both hover from 0.00 to 0.03 s system > time. I could barely tell the difference even averaged over 100 runs; I was > getting an average around 0.007 (system time) and 2.5s user time for both > methods. It's almost always better to use a high precision tim

Re: [R] speeding up "sum of squared differences" calculation

2013-10-22 Thread Bos, Roger
lison Cc: r-help@r-project.org Subject: Re: [R] speeding up "sum of squared differences" calculation Actually, you don't need to use c() in the dist example, either, which should shave off a few microseconds for a function call. It was late last night ... Ken On 22-10-2013 15

Re: [R] speeding up "sum of squared differences" calculation

2013-10-22 Thread Kenneth Knoblauch
Actually, you don't need to use c() in the dist example, either, which should shave off a few microseconds for a function call. It was late last night ... Ken On 22-10-2013 15:08, S Ellison wrote: Conclusion: hard to beat outer() for this purpose in R ... unless you use Ken Knoblauch's sugg

Re: [R] speeding up "sum of squared differences" calculation

2013-10-22 Thread S Ellison
> Conclusion: hard to beat outer() for this purpose in R ... unless you use Ken Knoblauch's suggestion of dist() or Rccp. Nice indeed. S Ellison *** This email and any attachments are confidential. Any use...{{dropped:8}} __

Re: [R] speeding up "sum of squared differences" calculation

2013-10-22 Thread S Ellison
> I am using a sum of squared differences in the objective function of an > optimization problem I am doing and I have managed to speed it up using > the outer function versus the nested for loops, but my suspicion is > that the calculation could be done even quicker. Please see the code > below f

Re: [R] speeding up

2013-10-21 Thread Ken Knoblauch
Ken Knoblauch inserm.fr> writes: > > Bos, Roger rothschild.com> writes: > > I am using a sum of squared differences in the > objective function of an optimization problem I am > doing and I > > have managed to speed it up using the > outer function versus the nested for loops, but my > susp

Re: [R] speeding up

2013-10-21 Thread Ken Knoblauch
Bos, Roger rothschild.com> writes: > I am using a sum of squared differences in the objective function of an optimization problem I am doing and I > have managed to speed it up using the outer function versus the nested for loops, but my suspicion is that > the calculation could be done even q

[R] speeding up "sum of squared differences" calculation

2013-10-21 Thread Bos, Roger
All, I am using a sum of squared differences in the objective function of an optimization problem I am doing and I have managed to speed it up using the outer function versus the nested for loops, but my suspicion is that the calculation could be done even quicker. Please see the code below fo

Re: [R] speeding up a loop

2013-10-18 Thread Ye Lin
when the system locks up it is consuming CPU to ~50%-60%, physical memory is about ~33%. I did tried on a subset of the data, e.g i in 1:100 and it works although takes about 5-10 min. Thanks for your help! On Fri, Oct 18, 2013 at 12:49 PM, jim holtman wrote: > When the system locks up, what

Re: [R] speeding up a loop

2013-10-18 Thread jim holtman
When the system locks up, what do you see in the Task Manager? Is it consuming CPU and memory? On the example data you sent, you won't get a match on the time since there is not match for the first entry in df1 in the 'b' dataframe. This leads to an error that you are not checking for. Have yo

Re: [R] speeding up a loop

2013-10-18 Thread Ye Lin
Thanks for your advice Jim! I tried Rprof but since the code just freezes the system, I am not able to get results so far as I had to close R after waiting for a long time. I am confused that the same code would work differently on the same system. I tried out foreach package as well but didnt no

Re: [R] speeding up a loop

2013-10-18 Thread jim holtman
You might want to use the profiler (Rprof) on a subset of your code to see where time is being spent. Find a subet that runs for a minute, or so, and enable profiling for the test. Take a look and see which functions are taking the time. This will be a start. You can also watch the task monitor

Re: [R] speeding up a loop

2013-10-18 Thread Ye Lin
Thanks for your help David! I was running the same code the other day and it worked fine although it took a while as well. You are right that dff shud be df1 and maybe it's a portion of my data so it have an error of length =0. About CPU usage, I got it by clicking ctrl+alt+delete and it showed C

Re: [R] speeding up a loop

2013-10-17 Thread David Winsemius
On Oct 17, 2013, at 2:56 PM, Ye Lin wrote: > Hey R professionals, > > I have a large dataset and I want to run a loop on it basically creating a > new column which gathers information from another reference table. > > When I run the code, R just freezes and even does not response after 30min >

[R] speeding up a loop

2013-10-17 Thread Ye Lin
Hey R professionals, I have a large dataset and I want to run a loop on it basically creating a new column which gathers information from another reference table. When I run the code, R just freezes and even does not response after 30min which is really unusual. I tried sapply as well but does no

Re: [R] Speeding up time conversion

2012-09-27 Thread Prof Brian Ripley
Users forget how much is an OS service. This is OS X not R being slow. On a recent Linux box it takes about 90s. But at least you can easily parallelize it: see ?pvec in package parallel for one way to do this (and one way not to). If the file contain a high proportion of duplicates, making a

[R] Speeding up time conversion

2012-09-27 Thread Fisher Dennis
R 2.15.1 OS X.7.4 Colleagues, I have a large dataset (27773536 records, the file is several GB) that contains a column of date / time entries in the format: "2/1/2011 13:25:01" I need to convert these to numeric values (ideally in seconds; the origin [e.g., 1970-01-01] is not impor

Re: [R] Speeding up a loop

2012-07-23 Thread Rui Barradas
Hello, I think this is a boundary issue. In your op you've said "less" not "less than or equal to". Try using "<=" and ">=" to see what happens, I bet it solves it. Rui Barradas Em 23-07-2012 14:43, wwreith escreveu: 1.15 60 0.553555415 0.574892872 1.15 60 0.5

Re: [R] Speeding up a loop

2012-07-23 Thread wwreith
1.15 60 0.553555415 0.574892872 1.15 60 0.563183983 0.564029359 Shouldn't the function row out the second one, since it it higher in position 3 and lower in position 4 i.e. it should not all be yes? -- View this message in context: http://r.789695.n4.n

Re: [R] Speeding up a loop

2012-07-21 Thread Rui Barradas
Ok, sorry, I should have included some comments. The function is divided in three parts, 1. intro, 2. decision, 3. keep rows. Part 3 is the function keep(), internal to to.keep(). Let's start with 1. 1. Setup some variables first. 1.a) The variables 'a'. If the input object 'x' is a matrix this

Re: [R] Speeding up a loop

2012-07-21 Thread wwreith
Any chance I could ask for an idiots guide for function to.keep(x). I understand how to use it but not what some of the lines are doing. Comments would be extremely helpful. -- View this message in context: http://r.789695.n4.nabble.com/Speeding-up-a-loop-tp4637201p4637316.html Sent from the R

Re: [R] Speeding up a loop

2012-07-21 Thread Rui Barradas
Hello, Maybe it would have been better to start a new thread, if the question is different. To show that it's a continuation, the subject line could be extended with "part 2" or something like that. This solution runs in 3.6 hours. to.keep <- function(x){ keep <- function(i, env){

Re: [R] Speeding up a loop

2012-07-21 Thread wwreith
next for loop question. I need a loop that removes a row from a matrix if it is worse in positions 1,2,3,4 than another row in the matrix. right now my matrix is 503028x26. Rule to define worse position1 is smaller, position2 is smaller, position3 is higher, and position4 is smaller Example: r

Re: [R] Speeding up a loop

2012-07-20 Thread Richard M. Heiberger
whoops, backwards new.x <- x[!remove.set,] On Fri, Jul 20, 2012 at 2:51 PM, Richard M. Heiberger wrote: > This works to multiply the ith row of a by the ith value of b. > It might be what you can use > > a <- matrix(1:30, 6, 5) > b <- 1:6 > > a > a*b > > > > To simplify your code, I think you c

Re: [R] Speeding up a loop

2012-07-20 Thread Richard M. Heiberger
This works to multiply the ith row of a by the ith value of b. It might be what you can use a <- matrix(1:30, 6, 5) b <- 1:6 a a*b To simplify your code, I think you can do this---one multiplication xA <- x %*% A Now you can do the tests on xA and not have any matrix multiplications inside t

Re: [R] Speeding up a loop

2012-07-20 Thread wwreith
That is faster than what I was doing and reducing 15% of my iterations it still very helpful. Next question. I need to multiply each row x[i,] of the matrix x by another matrix A. Specifically for(i in 1:n) { If (x[i,]%*%A[,1]<.5 || x[i,]%*%A[,2]<42 || x[i,]%*%A[,3]>150) { x<-x[-i,] n<-n-1 }.

Re: [R] Speeding up a loop

2012-07-20 Thread Jean V Adams
"Reith, William [USA]" wrote on 07/20/2012 09:52:02 AM: > Would this matrix eat up memory making the rest of my program > slower? Each x needs to be multiplied by a matrix and the results > checked against a set of thresholds. Doing them one at a time takes > at least 24 hours right now. >

Re: [R] Speeding up a loop

2012-07-20 Thread Petr Savicky
On Fri, Jul 20, 2012 at 04:26:34PM +0200, Petr Savicky wrote: > On Fri, Jul 20, 2012 at 05:45:30AM -0700, wwreith wrote: > > General problem: I have 20 projects that can be invested in and I need to > > decide which combinations meet a certain set of standards. The total > > possible combinations c

Re: [R] Speeding up a loop

2012-07-20 Thread Jean V Adams
Petr, This is great. MUCH faster than the code I provided. And much more elegant code. Thanks for posting! Jean Petr Savicky wrote on 07/20/2012 09:26:34 AM: > On Fri, Jul 20, 2012 at 05:45:30AM -0700, wwreith wrote: > > General problem: I have 20 projects that can be invested in and I need

Re: [R] Speeding up a loop

2012-07-20 Thread Petr Savicky
On Fri, Jul 20, 2012 at 05:45:30AM -0700, wwreith wrote: > General problem: I have 20 projects that can be invested in and I need to > decide which combinations meet a certain set of standards. The total > possible combinations comes out to 2^20. However I know for a fact that the > number of proje

Re: [R] Speeding up a loop

2012-07-20 Thread Jean V Adams
I've had to do something similar, so I wrote a small function to help. This runs in about 1/4 the time of your code on my machine. Others may have a more efficient approach. all.combs <- function(num, from=0, to=num) { # create a matrix of all possible combinations of num items # r

[R] Speeding up a loop

2012-07-20 Thread wwreith
General problem: I have 20 projects that can be invested in and I need to decide which combinations meet a certain set of standards. The total possible combinations comes out to 2^20. However I know for a fact that the number of projects must be greater than 5 and less than 13. So far the the code

Re: [R] Speeding up lots of calls to GLM

2012-03-12 Thread Thomas Lumley
On Tue, Mar 13, 2012 at 9:39 AM, Davy wrote: > Thanks for the reply. Sorry to be a pain, but could perhaps explain what you > mean by > "you can center each SNP variable at its mean to make the interaction > term uncorrelated with the main effects". Suppose rs1 and rs2 are your SNPs rs1cent <- r

Re: [R] Speeding up lots of calls to GLM

2012-03-12 Thread Davy
Thanks for the reply. Sorry to be a pain, but could perhaps explain what you mean by "you can center each SNP variable at its mean to make the interaction term uncorrelated with the main effects". I'm coming from a web based programming background so often statistical terminology doesn't click with

Re: [R] Speeding up lots of calls to GLM

2012-03-12 Thread Thomas Lumley
On Tue, Mar 13, 2012 at 7:11 AM, Davy wrote: > Dear useRs, > > First off, sorry about the long post. Figured it's better to give context > to get good answers (I hope!). Some time ago I wrote an R function that > will get all pairwise interactions of variables in a data frame. This > worked fine a

[R] Speeding up lots of calls to GLM

2012-03-12 Thread Davy
Dear useRs, First off, sorry about the long post. Figured it's better to give context to get good answers (I hope!). Some time ago I wrote an R function that will get all pairwise interactions of variables in a data frame. This worked fine at the time, but now a colleague would like me to do this

Re: [R] Speeding up "accumulation" code in large matrix calc?

2012-02-24 Thread robertfeldt
Wow! Thanks to both Petr and Berend for this extensive help. I learned a lot not only about this specific case but about R in general from studying your answers. The compiled version t4 seems to give the most consistently quickest results and for my case (about 6000 rows and 500 columns with a

Re: [R] Speeding up "accumulation" code in large matrix calc?

2012-02-24 Thread Petr Savicky
On Fri, Feb 24, 2012 at 09:06:28PM +0100, Berend Hasselman wrote: [...] > Summary of results > > > Nrow <- 100 > > Ncol <- 1000 > > A <- matrix((runif(Ncol*Nrow)<0.2)+0, nrow=Nrow) > > print(rbind(z1,z2,z3,z4,z5,z6)) >user.self sys.self elapsed user.child sys.child > z1 0.5430.005 0.

Re: [R] Speeding up "accumulation" code in large matrix calc?

2012-02-24 Thread Petr Savicky
On Fri, Feb 24, 2012 at 09:06:28PM +0100, Berend Hasselman wrote: [...] > I've done some speed tests. > > t1 <- function(m, outcome) { > nrows <- dim(m)[1] > ncols <- dim(m)[2] > res <- matrix(rep.int(0, nrows*ncols), nrow=nrows) > for(row in 1:nrows) { > for(

Re: [R] Speeding up "accumulation" code in large matrix calc?

2012-02-24 Thread Berend Hasselman
On 24-02-2012, at 20:02, Petr Savicky wrote: > On Fri, Feb 24, 2012 at 08:59:44AM -0800, robertfeldt wrote: >> Hi, >> >> I have R code like so: >> >> num.columns.back.since.last.occurence <- function(m, outcome) { >> nrows <- dim(m)[1]; >> ncols <- dim(m)[2]; >> res <- matrix(rep

Re: [R] Speeding up "accumulation" code in large matrix calc?

2012-02-24 Thread Petr Savicky
On Fri, Feb 24, 2012 at 08:59:44AM -0800, robertfeldt wrote: > Hi, > > I have R code like so: > > num.columns.back.since.last.occurence <- function(m, outcome) { > nrows <- dim(m)[1]; > ncols <- dim(m)[2]; > res <- matrix(rep.int(0, nrows*ncols), nrow=nrows); > for(row in

Re: [R] Speeding up "accumulation" code in large matrix calc?

2012-02-24 Thread Petr Savicky
On Fri, Feb 24, 2012 at 08:59:44AM -0800, robertfeldt wrote: > Hi, > > I have R code like so: > > num.columns.back.since.last.occurence <- function(m, outcome) { > nrows <- dim(m)[1]; > ncols <- dim(m)[2]; > res <- matrix(rep.int(0, nrows*ncols), nrow=nrows); > for(row in

[R] Speeding up "accumulation" code in large matrix calc?

2012-02-24 Thread robertfeldt
Hi, I have R code like so: num.columns.back.since.last.occurence <- function(m, outcome) { nrows <- dim(m)[1]; ncols <- dim(m)[2]; res <- matrix(rep.int(0, nrows*ncols), nrow=nrows); for(row in 1:nrows) { for(col in 2:ncols) {

[R] Speeding up "Multinomial Logit"/"Proportional odds model" in R vs stata?

2011-04-11 Thread Tal Galili
Hi all, An R blogger just published a comparison between R and stata for performing: - Multinomial Logit - Proportional odds model - Generalized Logit At: http://ekonometrics.blogspot.com/2011/04/speeding-tickets-for-r-and-stata.html The benchmark used (as mentioned in the comment to t

Re: [R] speeding up regressions using ddply

2010-09-22 Thread Greg Snow
rom: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > project.org] On Behalf Of Alison Macalady > Sent: Wednesday, September 22, 2010 5:05 AM > To: r-help@r-project.org > Subject: [R] speeding up regressions using ddply > > > > Hi, > > I have a data set that I&#

Re: [R] speeding up regressions using ddply

2010-09-22 Thread Abhijit Dasgupta, PhD
There has been a recent addition of parallel processing capabilities to plyr (I believe v1.2 and later), along with a dataframe iterator construct. Both have improved performance of ddply greatly for multicore/cluster computing. So we now have the niceness of plyr's grammar with pretty good pe

Re: [R] speeding up regressions using ddply

2010-09-22 Thread Ista Zahn
Hi Alison, On Wed, Sep 22, 2010 at 11:05 AM, Alison Macalady wrote: > > > Hi, > > I have a data set that I'd like to run logistic regressions on, using ddply > to speed up the computation of many models with different combinations of > variables. In my experience ddply is not particularly fast.

[R] speeding up regressions using ddply

2010-09-22 Thread Alison Macalady
Hi, I have a data set that I'd like to run logistic regressions on, using ddply to speed up the computation of many models with different combinations of variables. I would like to run regressions on every unique two-variable combination in a portion of my data set, but I can't quite

Re: [R] Speeding up prediction of survival estimates when using `survifit'

2010-08-31 Thread Frank Harrell
Frank E Harrell Jr Professor and ChairmanSchool of Medicine Department of Biostatistics Vanderbilt University On Mon, 30 Aug 2010, Ravi Varadhan wrote: Hi, I fit a Cox PH model to estimate the cause-specific hazards (in a competing risks setting). Then , I

[R] Speeding up prediction of survival estimates when using `survifit'

2010-08-30 Thread Ravi Varadhan
Hi, I fit a Cox PH model to estimate the cause-specific hazards (in a competing risks setting). Then , I compute the survival estimates for all the individuals in my data set using the `survfit' function. I am currently playing with a data set that has about 6000 observations and 12 covariate

[R] Speeding up the transfer of a tcl character array to R

2010-06-17 Thread Gabor Grothendieck
What is the best way to transfer a character array from tcl to R. 1. This works but its very slow if the tcl variable, r, is long: n <- as.numeric(tclvalue(.Tcl("llength $r"))) sapply(seq(0, length = n), function(i) tclvalue(.Tcl(paste("lindex $r", i 2. This also works and is fast but having

[R] Speeding up a bootstrap routine

2009-08-10 Thread babelproofreader
I have written the R code below to perform White's Data Mining Reality Check (DMRC) but as it stands at the moment it is painfully slow. It is written as a function as I call it many times from a script file with different data input, and the output is sunk() to a text file. Could anyone suggest i

Re: [R] Speeding up casting a dataframe from long to wide format

2008-12-03 Thread hadley wickham
Hi Daren, Unfortunately, the current version of reshape isn't very efficient. I'm working on a new version which should be 10-20x times faster for the operation that you're performing, but this won't be ready for a while and in the meantime you might want to try an alternative approach, like the o

Re: [R] Speeding up casting a dataframe from long to wide format

2008-12-03 Thread Gabor Grothendieck
Try timing this to see if its any faster: > lev <- levels(wer$Predictor) > out <- outer(wer$Predictor, lev, "==") > colnames(out) <- lev > aggregate(out, wer[1:2], sum) Name Type A B 11a 1 0 22b 1 0 33c 1 0 44d 1 1 55e 1 1 On Tue, Dec 2, 2008 at 11:52 PM

[R] Speeding up casting a dataframe from long to wide format

2008-12-03 Thread Daren Tan
Hi, I am casting a dataframe from long to wide format. The same codes that works for a smaller dataframe would take a long time (more than two hours and still running) for a longer dataframe of 2495227 rows and ten different predictors. How to make it more efficient ? wer <- data.frame(Nam

[R] speeding up multiple kriging predictions using fields package

2008-08-27 Thread Felix Bonowski
Sorry for the double post - I accidentially hit my the "send" button... We want to build a global optimizer for extremely expensive noisy objective functions based on physical measurements. Our approach is based on optimizing kriging response surfaces surface models. (as seen in www.cs.ubc.ca/~h

Re: [R] speeding up loop and dealing wtih memory problems

2008-07-28 Thread Prof Brian Ripley
We were not told this was a matrix, rather a 'dataset'. If it is matrix, logical indexing via is.na(x) is pretty good, although it will create an index equal in size to the dataset (but logical). If 'dataset' means a data frame, you will use less memory using a for() loop over columns, e.g.

Re: [R] speeding up loop and dealing wtih memory problems

2008-07-28 Thread jim holtman
If your matrix is 835353x86, then if it is numeric, then it will take about 550MB for a single copy. You should therefore have at least 2GB (so you can have a couple of copies as part of some processing) of real memory on your system. If you want to replace NAs with zero, then this is how you mig

Re: [R] speeding up loop and dealing wtih memory problems

2008-07-28 Thread ONKELINX, Thierry
L PROTECTED] Namens Denise Xifara Verzonden: maandag 28 juli 2008 15:15 Aan: r-help@r-project.org Onderwerp: [R] speeding up loop and dealing wtih memory problems Dear All and Mark, Given a dataset that I have called dat, I was hoping to speed up the following loop: for(i in 1:835353){ for(j in 1:86){

Re: [R] speeding up loop and dealing wtih memory problems

2008-07-28 Thread David Hajage
Is it what you want ? > x <- matrix(c(1:3, NA, NA, 4, 1:2, NA), 3, 3) > x [,1] [,2] [,3] [1,]1 NA1 [2,]2 NA2 [3,]34 NA > x[is.na(x)] <- 0 > x [,1] [,2] [,3] [1,]101 [2,]202 [3,]340 2008/7/28 Denise Xifara <[EMAIL PROTECT

[R] speeding up loop and dealing wtih memory problems

2008-07-28 Thread Denise Xifara
Dear All and Mark, Given a dataset that I have called dat, I was hoping to speed up the following loop: for(i in 1:835353){ for(j in 1:86){ if (is.na(dat[i,j])==TRUE){dat[i,j]<-0 }}} Actually I am also having a memory problem. I get the following: Error: cannot allocate vector of size 3.2 Mb

Re: [R] speeding up a special product of three arrays

2008-05-09 Thread Vincent Goulet
] > To: Sent: Thursday, May 08, 2008 11:20 PM Subject: [R] speeding up a special product of three arrays I am struggling with R code optimization, a recurrent topic on this list. I have three arrays, say A, B and C, all having the same number of columns. I need to compute an array D

Re: [R] speeding up a special product of three arrays

2008-05-09 Thread Dimitris Rizopoulos
p://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: "Giuseppe Paleologo" <[EMAIL PROTECTED]> To: Sent: Thursday, May 08, 2008 11:20 PM Subject: [R] speeding up a special product of three arrays I am struggling with R code optimization, a recurrent topi

Re: [R] speeding up a special product of three arrays

2008-05-08 Thread Vincent Goulet
Le jeu. 8 mai à 17:20, Giuseppe Paleologo a écrit : I am struggling with R code optimization, a recurrent topic on this list. I have three arrays, say A, B and C, all having the same number of columns. I need to compute an array D whose generic element is D[i, j, k] <- sum_n A[i, n]*B[j,

[R] speeding up a special product of three arrays

2008-05-08 Thread Giuseppe Paleologo
I am struggling with R code optimization, a recurrent topic on this list. I have three arrays, say A, B and C, all having the same number of columns. I need to compute an array D whose generic element is D[i, j, k] <- sum_n A[i, n]*B[j, n]*C[k, n] Cycling over the three indices and subsetting th

Re: [R] speeding up likelihood computation

2007-12-03 Thread Deepankar Basu
Jim and others, As far as I can see the computation of certain conditional probabilities (required for computing my likelihood function) is what is slowing down the evaluation of the likelihood function. Let me explain what these conditional probabilities are and how I am (no doubt inefficiently)

Re: [R] speeding up likelihood computation

2007-12-03 Thread Deepankar Basu
Hi Jim, Thanks for your suggestion. My guess is that most of the time is taken by various kinds of assignments that I am making using loops; and these can be done without loops. In a follow-up post, I will try to explain in greater detail each part of my code (especially the parts where assignment

Re: [R] speeding up likelihood computation

2007-12-02 Thread jim holtman
One thing that I would suggest that you do is to use Rprof on a subset of the data that runs for 10-15 minutes and see where some of the hot spots are. Since you have not provided commented, minimal, self-contained, reproducible code, it is hard to determine where the inefficiencies are since we d

[R] speeding up likelihood computation

2007-12-02 Thread DEEPANKAR BASU
R Users: I am trying to estimate a model of fertility behaviour using birth history data with maximum likelihood. My code works but is extremely slow (because of several for loops and my programming inefficiencies); when I use the genetic algorithm to optimize the likelihood function, it takes

Re: [R] Speeding up simulation of mean nearest neighbor distances

2007-10-03 Thread hadley wickham
It looks like you can pass a vector of neighbourhoods to nndist. nndist(rpp, k=2:10) Although you get some warnings, the answers appear to be the same. all.equal(t(sapply(2:10, function(i) nndist(rpp, k=i))), nndist(rpp, k=2:10)) This might be quite a lot faster, depending on how much work is c

Re: [R] Speeding up simulation of mean nearest neighbor distances

2007-10-03 Thread jim holtman
If you take a look at what is happening with Rprof, you will see that most of the time (96.6%) is being taken in the 'nndist' function, so if you want to improve your algorithm, can you somehow reduce the number of time you call it, or find a different approach. So it is a function of the algorith

[R] Speeding up simulation of mean nearest neighbor distances

2007-10-03 Thread Dale Steele
I've written the function below to simulate the mean 1st through nth nearest neighbor distances for a random spatial pattern using the functions nndist() and runifpoint() from spatsat. It works, but runs relatively slowly - would appreciate suggestions on how to speed up this function. Thanks. -

Re: [R] Speeding up image plots

2007-10-01 Thread Scionforbai
> I wonder whether it is possible to speed up plots of 2-dimensional > images (such as image() or levelplot()) in order to export lower quality > versions of the resulting plots. > But the underlying arrays are > so big (900x900 entries) that the export of every single image takes > very long. I

[R] Speeding up image plots

2007-10-01 Thread Maik Heistermann
Dear R-users, I wonder whether it is possible to speed up plots of 2-dimensional images (such as image() or levelplot()) in order to export lower quality versions of the resulting plots. I intend to export a large number of images in order to construct animated gifs. For this purpose, I used t