Re: [R] Replace a for loop with a function

2011-09-19 Thread Jean V Adams
Eekhout, I. wrote on 09/19/2011 10:16:17 AM: > > Hi all, > > I would like to replace the for loop in the code below with a function > to improve the speed and to make the script more efficient. > The loop creates a vector of integers (x) with the probability of f for > each integer. > The lengt

Re: [R] Replace a for loop with a function

2011-09-19 Thread Eik Vettorazzi
sorry, there is a superfluous bracket at the end of the line, it should be read as sample(1:3,50,prob=c(0.5,0.15,0.35),replace=T) Am 19.09.2011 19:08, schrieb Eik Vettorazzi: > Hi Iris, > maybe I misinterpret this, but I think in the end it all comes down to > sample(1:3,50,prob=c(0.5,0.15,0.35),r

Re: [R] Replace a for loop with a function

2011-09-19 Thread Eik Vettorazzi
Hi Iris, maybe I misinterpret this, but I think in the end it all comes down to sample(1:3,50,prob=c(0.5,0.15,0.35),replace=T)) cheers Am 19.09.2011 17:16, schrieb Eekhout, I.: > Hi all, > > I would like to replace the for loop in the code below with a function > to improve the speed and to mak

Re: [R] replace a for loop

2010-02-10 Thread Ivan Calandra
Well actually, in that case, I don't think it is really necessary to replace the loop because it is quite fast with the small size I have. But I thought it would help me to understand how would vectorization work and how to use the *apply functions. Thanks anyway for your piece of advice! Ivan

Re: [R] replace a for loop

2010-02-10 Thread Petr PIKAL
Hi how many objects do you have in a list? Loop is ineffective if you use several nested loops and/or there is some unnecessary mimic of simple function. e.g. you can use this set.seed(666) x <- runif(10) vysled <- 0 for(i in 1:length(x)) { if(vysled > x[i]) vysled <- vysled else vysled <- x[i

Re: [R] replace a for loop

2010-02-10 Thread Ivan Calandra
After reading the R news, I've tried this code and it works: > rapply(list(names(test),test), write.csv, file="filename.csv", append=T, row.names=F) However, the output is structured like this: names(test)[[1]] names(test)[[2]] etc... test[[1]] test[[2]] etc... I would like to alternate names(

Re: [R] replace a for loop

2010-02-10 Thread Liviu Andronic
On Wed, Feb 10, 2010 at 9:50 AM, Ivan Calandra wrote: > I'm still quite new in R and I don't really understand this whole > "vectorization" thing. > See [1] for an article on vectorisation and loops in R. Liviu [1] http://www.r-project.org/doc/Rnews/Rnews_2008-1.pdf _

Re: [R] replace a for loop with lapply or relative

2010-02-04 Thread jim holtman
Have you considered using a different data structure: > # change the data structure > x <- data.frame( + type=rep(c('x1', 'x2', 'x3'), each=100), + high=c(d[,4], d[,5], d[,6]), + value=c(d[,1], d[,2], d[,3])) > head(x) type high value 1 x10 0.8936737 2 x10 -1.047298