Re: [R] vectorisation

2013-02-03 Thread Berend Hasselman
On 02-02-2013, at 17:38, Brett Robinson wrote: > Hi > I'm trying to set up a simulation problem without resorting to (m)any loops. > I want to set entries in a data frame of zeros ('starts' in the code below) > to 1 at certain points and the points have been randomly generated and stored > in

Re: [R] vectorisation

2013-02-02 Thread Rui Barradas
Hello, Try the following. set.seed(4315) ml <- data.frame(matrix(sample(1:50,80, replace=TRUE),20,4)) mm <- apply(ml, 2, cumsum) s2 <- starts <- data.frame(matrix(0,600,4)) for (i in 1:4){ starts[,i][mm[,i]] <- 1 } s2[] <- lapply(seq_len(ncol(mm)), function(i) {s2[,i][mm[,i]] <- 1; s2[

Re: [R] vectorisation

2013-02-02 Thread arun
Hi, Not sure this helps: ml <- data.frame(matrix(sample(1:50,80, replace=TRUE),20,4)) mm <- apply(ml, 2, cumsum) starts<- data.frame(matrix(0,600,4)) starts1<- data.frame(matrix(0,600,4)) for (i in 1:4){ starts1[,i][mm[,i]] <-1 } starts2<-as.data.frame(do.call(cbind,lapply(1:4,function(i) {star

Re: [R] vectorisation?

2010-07-26 Thread Raghu
Many thanks Dimitris. On Mon, Jul 26, 2010 at 9:13 AM, Dimitris Rizopoulos < d.rizopou...@erasmusmc.nl> wrote: > have a look at function rollapply() from package zoo. > > I hope it helps. > > Best, > Dimitris > > > On 7/26/2010 8:28 AM, Raghu wrote: > >> Hi >> >> I have 3500 rows of data (say a s

Re: [R] vectorisation?

2010-07-26 Thread Dimitris Rizopoulos
have a look at function rollapply() from package zoo. I hope it helps. Best, Dimitris On 7/26/2010 8:28 AM, Raghu wrote: Hi I have 3500 rows of data (say a single column) in a vector. If I want to compare every ith element with the simple average of the previous (i-5) elements, then I could