Re: [R] apply and cousins

2016-06-09 Thread John Logsdon
Thanks Jim and others (and sorry Jim - an early version of this slipped into your inbox :)) Apologies for not giving some concrete code - I was trying to explain in words. What I need to do is to fit a simple linear model to successive sections of a long matrix. So far, the best solution I have

Re: [R] apply and cousins

2016-06-08 Thread Jim Lemon
Hi John, With due respect to the other respondents, here is something that might help: # get a vector of values foo<-rnorm(100) # get a vector of increasing indices (aka your "recent" values) bar<-sort(sample(1:100,40)) # write a function to "clump" the adjacent index values clump_adj_int<-functio

Re: [R] apply and cousins

2016-06-08 Thread MacQueen, Don
Hopefully Bert and William won't be offended if I more or less summarize: Are you assuming a loop will take ages, or have you actually tested it? I wouldn't assume a loop will take ages, or that it will take much longer than apply(). What's wrong with apply( X[ {logical expression } , ] , 1, f

Re: [R] apply and cousins

2016-06-08 Thread Bert Gunter
John: 1. Please read and follow the posting guide. In particular, provide a small reproducible example so that we know what your data and looping code look like. 2. apply-type commands are *not* vectorized; they are disguised loops that may or may not offer any speedup over explicit loops. 3. A

Re: [R] apply and cousins

2016-06-08 Thread William Dunlap via R-help
>It is easy in a loop but that will take ages. Is there any vectorised >apply-like solution to this? If you showed the loop that takes ages, along with small inputs for it (and an indication of how to expand those small inputs to big ones), someone might be able to show you some code that does the

[R] apply and cousins

2016-06-08 Thread John Logsdon
Folks Is there any way to get the row index into apply as a variable? I want a function to do some sums on a small subset of some very long vectors, rolling through the whole vectors. apply(X,1,function {do something}, other arguments) seems to be the way to do it. The subset I want is the mos