Re: [R] nested for loops too slow

2015-04-12 Thread Bert Gunter
Well, sort of...

aggregate() is basically a wrapper for lapply(), which ultimately must loop
over the function call at the R interpreter level, as opposed to vectorized
functions that loop at the C level and hence can be orders of magnitude
faster. As a result, there is often little difference in efficiency between
explicit and *smart* (in the sense that Pat Burns has already pointed out
of not growing structures at each iteration,among other things)  for()
looping and apply-type calls. For some of us, the chief advantage of the
*apply idioms is that the code is more readable and maintainable, with R
handling fussy details of loop indexing, for example.lapply() is also more
in keeping with the functional programming paradigm.
​Others​
find both these "virtues" to be annoyances,
​ ​
however, and prefer explicit *smart* looping. Chaque un á
​ ​
son goû
​t​
.

None of which necessarily denies the wisdom of the approach you've
suggested, however. It may indeed be considerably faster,
but timing will have to tell. I am just trying to correct
​(again) ​
the
​ ​
widely held misperception
​that yo​
u
​ seem to​
express
​.​


Cheers,
Bert


Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge is
certainly not wisdom."
Clifford Stoll



On Sun, Apr 12, 2015 at 1:48 PM, Thierry Onkelinx 
wrote:

> You don't need loops at all.
>
> grw <- aggregate(gw ~ ts + ISEG + iter, data = dat, FUN = sum)
> GRW <- aggregate(gw ~ ts + ISEG, data = grw, FUN = function(x){max(x) -
> min(x)})
> DC <- aggregate(div ~ ts + ISEG, data = subset(dat, IRCH == 1), FUN =
> function(x){max(x) - min(x)})
> iter <- aggregate(iter ~ ts + ISEG, data = subset(dat, IRCH == 1), FUN
> = max)
> tmp <- merge(DC, iter)
> merge(tmp, GRW)
>
> another option is to use the plyr package
>
> library(plyr)
> merge(
>   ddply(
> subset(dat, IRCH == 1),
> c("ts", "ISEG"),
> summarize,
> divChng = max(div) - min(div),
> max.iter = max(iter)
>   ),
>   ddply(
> dat,
> c("ts", "ISEG"),
> summarize,
> gwChng = diff(range(ave(gw, iter, FUN = sum)))
>   )
> )
>
> Best regards,
>
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
> Forest
> team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
> Kliniekstraat 25
> 1070 Anderlecht
> Belgium
>
> To call in the statistician after the experiment is done may be no more
> than asking him to perform a post-mortem examination: he may be able to say
> what the experiment died of. ~ Sir Ronald Aylmer Fisher
> The plural of anecdote is not data. ~ Roger Brinner
> The combination of some data and an aching desire for an answer does not
> ensure that a reasonable answer can be extracted from a given body of data.
> ~ John Tukey
>
> 2015-04-12 15:47 GMT+02:00 Morway, Eric :
>
> > The small example below works lighting-fast; however, when I run the same
> > script on my real problem, a 1Gb text file, the for loops have been
> running
> > for over 24 hrs and I have no idea if the processing is 10% done or 90%
> > done.  I have not been able to figure out a betteR way to code up the
> > material within the for loops at the end of the example below.  The
> > contents of divChng, the final product, are exactly what I'm after, but I
> > need help formulating more efficient R script, I've got two more 1Gb
> files
> > to process after the current one finishes, whenever that is...
> >
> > I appreciate any insights/solutions, Eric
> >
> > dat <- read.table(textConnection("ISEG  IRCH  div  gw
> > 1  1  265  229
> > 1  2  260  298
> > 1  3  234  196
> > 54  1  432  485
> > 54  39  467  485
> > 54  40  468  468
> > 54  41  460  381
> > 54  42  489  502
> > 1  1  265  317
> > 1  2  276  225
> > 1  3  217  164
> > 54  1  430  489
> > 54  39  456  495
> > 54  40  507  607
> > 54  41  483  424
> > 54  42  457  404
> > 1  1  265  278
> > 1  2  287  370
> > 1  3  224  274
> > 54  1  412  585
> > 54  39  473  532
> > 54  40  502  595
> > 54  41  497  441
> > 54  42  447  467
> > 1  1  230  258
> > 1  2  251  152
> > 1  3  199  179
> > 54  1  412  415
> > 54  39  439  538
> > 54  40  474  486
> > 54  41  477  484
> > 54  42  413  346
> > 1  1  230  171
> > 1  2  262  171
> > 1  3  217  263
> > 54  1  432  485
> > 54  39  455  482
> > 54  40  493  419
> > 54  41  489  536
> > 54  42  431  504
> > 1  1  1002  1090
> > 1  2  1222  1178
> > 1  3  1198  1177
> > 54  1  1432  1485
> > 54  39  1876  1975
> > 54  40  1565  1646
> > 54  41  1455  1451
> > 54  42  1427  1524
> > 1  1  1002  968
> > 1  2  1246  1306
> > 1  3  1153  1158
> > 54  1  1532  1585
> > 54  39  1790  1889
> > 54  40  1490  1461
> > 54  41  1518  1536
> > 54  42  1486  1585
> > 1  1  1002  1081
> > 1  2  1229  1262
> > 1  3  1142  1241
> > 54  1  1632  1659
> > 54  39  1797  1730
> > 54  40  1517  1466
> > 54  41  1527  1589

Re: [R] nested for loops too slow

2015-04-12 Thread Thierry Onkelinx
You don't need loops at all.

grw <- aggregate(gw ~ ts + ISEG + iter, data = dat, FUN = sum)
GRW <- aggregate(gw ~ ts + ISEG, data = grw, FUN = function(x){max(x) -
min(x)})
DC <- aggregate(div ~ ts + ISEG, data = subset(dat, IRCH == 1), FUN =
function(x){max(x) - min(x)})
iter <- aggregate(iter ~ ts + ISEG, data = subset(dat, IRCH == 1), FUN
= max)
tmp <- merge(DC, iter)
merge(tmp, GRW)

another option is to use the plyr package

library(plyr)
merge(
  ddply(
subset(dat, IRCH == 1),
c("ts", "ISEG"),
summarize,
divChng = max(div) - min(div),
max.iter = max(iter)
  ),
  ddply(
dat,
c("ts", "ISEG"),
summarize,
gwChng = diff(range(ave(gw, iter, FUN = sum)))
  )
)

Best regards,

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

2015-04-12 15:47 GMT+02:00 Morway, Eric :

> The small example below works lighting-fast; however, when I run the same
> script on my real problem, a 1Gb text file, the for loops have been running
> for over 24 hrs and I have no idea if the processing is 10% done or 90%
> done.  I have not been able to figure out a betteR way to code up the
> material within the for loops at the end of the example below.  The
> contents of divChng, the final product, are exactly what I'm after, but I
> need help formulating more efficient R script, I've got two more 1Gb files
> to process after the current one finishes, whenever that is...
>
> I appreciate any insights/solutions, Eric
>
> dat <- read.table(textConnection("ISEG  IRCH  div  gw
> 1  1  265  229
> 1  2  260  298
> 1  3  234  196
> 54  1  432  485
> 54  39  467  485
> 54  40  468  468
> 54  41  460  381
> 54  42  489  502
> 1  1  265  317
> 1  2  276  225
> 1  3  217  164
> 54  1  430  489
> 54  39  456  495
> 54  40  507  607
> 54  41  483  424
> 54  42  457  404
> 1  1  265  278
> 1  2  287  370
> 1  3  224  274
> 54  1  412  585
> 54  39  473  532
> 54  40  502  595
> 54  41  497  441
> 54  42  447  467
> 1  1  230  258
> 1  2  251  152
> 1  3  199  179
> 54  1  412  415
> 54  39  439  538
> 54  40  474  486
> 54  41  477  484
> 54  42  413  346
> 1  1  230  171
> 1  2  262  171
> 1  3  217  263
> 54  1  432  485
> 54  39  455  482
> 54  40  493  419
> 54  41  489  536
> 54  42  431  504
> 1  1  1002  1090
> 1  2  1222  1178
> 1  3  1198  1177
> 54  1  1432  1485
> 54  39  1876  1975
> 54  40  1565  1646
> 54  41  1455  1451
> 54  42  1427  1524
> 1  1  1002  968
> 1  2  1246  1306
> 1  3  1153  1158
> 54  1  1532  1585
> 54  39  1790  1889
> 54  40  1490  1461
> 54  41  1518  1536
> 54  42  1486  1585
> 1  1  1002  1081
> 1  2  1229  1262
> 1  3  1142  1241
> 54  1  1632  1659
> 54  39  1797  1730
> 54  40  1517  1466
> 54  41  1527  1589
> 54  42  1514  1612"),header=TRUE)
>
> dat$seq <- ifelse(dat$ISEG==1 & dat$IRCH==1, 1, 0)
> tmp <- diff(dat[dat$seq==1,]$div)!=0
> dat$idx <- 0
> dat[dat$seq==1,][c(TRUE,tmp),]$idx <- 1
> dat$ts <- cumsum(dat$idx)
> dat$iter <- ave(dat$seq, dat$ts,FUN=cumsum)
> dat$ct <- seq(1:length(dat[,1]))
>
> timeStep <- unique(dat$ts)
> SEG <- unique(dat$ISEG)
> divChng <- data.frame(ts=NA, ISEG=NA, divChng=NA, gwChng=NA, iter=NA)
>
> #Can the following be rescripted for better harnessing R's processing
> power?
>
> for (i in 1:length(timeStep)){
>   for (j in 1:length(SEG)){
> datTS <- subset(dat,ts==timeStep[i] & ISEG==SEG[j] & IRCH==1)
> datGW <- subset(dat,ts==timeStep[i] & ISEG==SEG[j])
> grw <- aggregate(gw ~ iter, datGW, sum)
>
> DC <- max(datTS$div)-min(datTS$div)
> GRW <- max(grw$gw) - min(grw$gw)
> divChng <- rbind(divChng,c(datTS$ts[1], SEG[j], DC, GRW,
> max(datTS$iter)))
>   }
> }
> divChng <- divChng[!is.na(divChng$ISEG),]
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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

Re: [R] nested for loops too slow

2015-04-12 Thread J Robertson-Burns

You are certainly in Circle 2 of 'The R Inferno',
which I suspect is where almost all of the
computation time is coming from.

Instead of doing:

divChng <- rbind(divChng,c(datTS$ts[1], SEG[j], DC, GRW,
max(datTS$iter)))

it would be much better to create 'divChng' to
be the final length and then in the loop do:

divChng[count,] <- c(datTS$ts[1], SEG[j], DC, GRW,
max(datTS$iter))
count <- count + 1

You may also want to explore 'tapply'.
If this is something you will be doing
a lot of, then you should probably learn
the 'data.table' package.

http://www.burns-stat.com/documents/books/the-r-inferno/

Pat

On 12/04/2015 14:47, Morway, Eric wrote:

The small example below works lighting-fast; however, when I run the same
script on my real problem, a 1Gb text file, the for loops have been running
for over 24 hrs and I have no idea if the processing is 10% done or 90%
done.  I have not been able to figure out a betteR way to code up the
material within the for loops at the end of the example below.  The
contents of divChng, the final product, are exactly what I'm after, but I
need help formulating more efficient R script, I've got two more 1Gb files
to process after the current one finishes, whenever that is...

I appreciate any insights/solutions, Eric

dat <- read.table(textConnection("ISEG  IRCH  div  gw
1  1  265  229
1  2  260  298
1  3  234  196
54  1  432  485
54  39  467  485
54  40  468  468
54  41  460  381
54  42  489  502
1  1  265  317
1  2  276  225
1  3  217  164
54  1  430  489
54  39  456  495
54  40  507  607
54  41  483  424
54  42  457  404
1  1  265  278
1  2  287  370
1  3  224  274
54  1  412  585
54  39  473  532
54  40  502  595
54  41  497  441
54  42  447  467
1  1  230  258
1  2  251  152
1  3  199  179
54  1  412  415
54  39  439  538
54  40  474  486
54  41  477  484
54  42  413  346
1  1  230  171
1  2  262  171
1  3  217  263
54  1  432  485
54  39  455  482
54  40  493  419
54  41  489  536
54  42  431  504
1  1  1002  1090
1  2  1222  1178
1  3  1198  1177
54  1  1432  1485
54  39  1876  1975
54  40  1565  1646
54  41  1455  1451
54  42  1427  1524
1  1  1002  968
1  2  1246  1306
1  3  1153  1158
54  1  1532  1585
54  39  1790  1889
54  40  1490  1461
54  41  1518  1536
54  42  1486  1585
1  1  1002  1081
1  2  1229  1262
1  3  1142  1241
54  1  1632  1659
54  39  1797  1730
54  40  1517  1466
54  41  1527  1589
54  42  1514  1612"),header=TRUE)

dat$seq <- ifelse(dat$ISEG==1 & dat$IRCH==1, 1, 0)
tmp <- diff(dat[dat$seq==1,]$div)!=0
dat$idx <- 0
dat[dat$seq==1,][c(TRUE,tmp),]$idx <- 1
dat$ts <- cumsum(dat$idx)
dat$iter <- ave(dat$seq, dat$ts,FUN=cumsum)
dat$ct <- seq(1:length(dat[,1]))

timeStep <- unique(dat$ts)
SEG <- unique(dat$ISEG)
divChng <- data.frame(ts=NA, ISEG=NA, divChng=NA, gwChng=NA, iter=NA)

#Can the following be rescripted for better harnessing R's processing power?

for (i in 1:length(timeStep)){
   for (j in 1:length(SEG)){
 datTS <- subset(dat,ts==timeStep[i] & ISEG==SEG[j] & IRCH==1)
 datGW <- subset(dat,ts==timeStep[i] & ISEG==SEG[j])
 grw <- aggregate(gw ~ iter, datGW, sum)

 DC <- max(datTS$div)-min(datTS$div)
 GRW <- max(grw$gw) - min(grw$gw)
 divChng <- rbind(divChng,c(datTS$ts[1], SEG[j], DC, GRW,
max(datTS$iter)))
   }
}
divChng <- divChng[!is.na(divChng$ISEG),]

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


[R] nested for loops too slow

2015-04-12 Thread Morway, Eric
The small example below works lighting-fast; however, when I run the same
script on my real problem, a 1Gb text file, the for loops have been running
for over 24 hrs and I have no idea if the processing is 10% done or 90%
done.  I have not been able to figure out a betteR way to code up the
material within the for loops at the end of the example below.  The
contents of divChng, the final product, are exactly what I'm after, but I
need help formulating more efficient R script, I've got two more 1Gb files
to process after the current one finishes, whenever that is...

I appreciate any insights/solutions, Eric

dat <- read.table(textConnection("ISEG  IRCH  div  gw
1  1  265  229
1  2  260  298
1  3  234  196
54  1  432  485
54  39  467  485
54  40  468  468
54  41  460  381
54  42  489  502
1  1  265  317
1  2  276  225
1  3  217  164
54  1  430  489
54  39  456  495
54  40  507  607
54  41  483  424
54  42  457  404
1  1  265  278
1  2  287  370
1  3  224  274
54  1  412  585
54  39  473  532
54  40  502  595
54  41  497  441
54  42  447  467
1  1  230  258
1  2  251  152
1  3  199  179
54  1  412  415
54  39  439  538
54  40  474  486
54  41  477  484
54  42  413  346
1  1  230  171
1  2  262  171
1  3  217  263
54  1  432  485
54  39  455  482
54  40  493  419
54  41  489  536
54  42  431  504
1  1  1002  1090
1  2  1222  1178
1  3  1198  1177
54  1  1432  1485
54  39  1876  1975
54  40  1565  1646
54  41  1455  1451
54  42  1427  1524
1  1  1002  968
1  2  1246  1306
1  3  1153  1158
54  1  1532  1585
54  39  1790  1889
54  40  1490  1461
54  41  1518  1536
54  42  1486  1585
1  1  1002  1081
1  2  1229  1262
1  3  1142  1241
54  1  1632  1659
54  39  1797  1730
54  40  1517  1466
54  41  1527  1589
54  42  1514  1612"),header=TRUE)

dat$seq <- ifelse(dat$ISEG==1 & dat$IRCH==1, 1, 0)
tmp <- diff(dat[dat$seq==1,]$div)!=0
dat$idx <- 0
dat[dat$seq==1,][c(TRUE,tmp),]$idx <- 1
dat$ts <- cumsum(dat$idx)
dat$iter <- ave(dat$seq, dat$ts,FUN=cumsum)
dat$ct <- seq(1:length(dat[,1]))

timeStep <- unique(dat$ts)
SEG <- unique(dat$ISEG)
divChng <- data.frame(ts=NA, ISEG=NA, divChng=NA, gwChng=NA, iter=NA)

#Can the following be rescripted for better harnessing R's processing power?

for (i in 1:length(timeStep)){
  for (j in 1:length(SEG)){
datTS <- subset(dat,ts==timeStep[i] & ISEG==SEG[j] & IRCH==1)
datGW <- subset(dat,ts==timeStep[i] & ISEG==SEG[j])
grw <- aggregate(gw ~ iter, datGW, sum)

DC <- max(datTS$div)-min(datTS$div)
GRW <- max(grw$gw) - min(grw$gw)
divChng <- rbind(divChng,c(datTS$ts[1], SEG[j], DC, GRW,
max(datTS$iter)))
  }
}
divChng <- divChng[!is.na(divChng$ISEG),]

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] nested "for" loops

2011-11-06 Thread nick_pan
I sent a post yesterday  that I found out why my function didn't work. It's
ok now it works.

Thank you all.


--
View this message in context: 
http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3994996.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] nested "for" loops

2011-11-05 Thread R. Michael Weylandt
No idea how this relates to what you said originally but glad you got
it all worked out.

And let us all reiterate: really, don't use nested for loops...there's
a better way: promise!

Michael

On Sat, Nov 5, 2011 at 2:20 PM, nick_pan  wrote:
> I found the way out - it was because the borders of the vectors was close
> enough thats why I had the same result while I was adding points to the
> sequence. The example I gave was irrelevant but I made in order to find out
> that the problem was.
> Thank you all for your answers.
>
>
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3993917.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@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.
>

__
R-help@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.


Re: [R] nested "for" loops

2011-11-05 Thread Jeff Newmiller
Bert, this is not helpful. Since for loops and apply functions are not 
vectorized, why are you admonishing Carl that vectorizing doesn't always speed 
up algorithms? He didn't reference apply functions as being vectorized. But you 
seem to be doing so.

I would assert that vectorizing DOES always speed up algorithms, but things 
people sometimes think are vectorizing are not really.
---
Jeff Newmiller The . . Go Live...
DCN: Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

Bert Gunter  wrote:

Carl: "Almost anything you can do in a for() loop can be done easier and
faster with vectorization.-- "

That is false: while this is certainly true for a great many basic
vectorized operations, it is certainly false for most other things --
simulations are a typical example. Note that __ply type operations in base
R, plyr , and other packages are (generally) _not_vectorized; they are
"disguised" loops. Explicit for() loops are often just as fast or even a
bit faster, though many of us prefer what we consider the more transparent
functional style code of the __ply's.

Cheers,
Bert

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

[[alternative HTML version deleted]]

_

R-help@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.


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] nested "for" loops

2011-11-05 Thread nick_pan
I found the way out - it was because the borders of the vectors was close
enough thats why I had the same result while I was adding points to the
sequence. The example I gave was irrelevant but I made in order to find out
that the problem was.
Thank you all for your answers.


--
View this message in context: 
http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3993917.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] nested "for" loops

2011-11-05 Thread Bert Gunter
Carl: "Almost anything you can do in a for() loop can be done easier and
faster with vectorization.-- "

That is false: while this is certainly true for a great many basic
vectorized operations, it is certainly false for most other things --
simulations are a typical example. Note that __ply type operations in base
R, plyr , and other packages are (generally) _not_vectorized; they are
"disguised" loops. Explicit for() loops are often just as fast or even a
bit faster, though many of us prefer what we consider the more transparent
functional style code of the __ply's.

Cheers,
Bert

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

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] nested "for" loops

2011-11-05 Thread David Winsemius
You need to define "l" as a dimensioned object , either a vector or an array, 
and then assign the value of your calculation to the correctly indexed 
"location" in that object. Otherwise you are just overwriting the value each 
time through the loop. Use these help pages (and review "Introduction to R"

?array
?vector
?"["

On Nov 4, 2011, at 7:49 PM, nick_pan  wrote:

> Hi all , I have written a code with nested "for" loops . 
> The aim is to estimate the maximum likelihood by creating 3 vectors with the
> same length( sequence ) 
> and then to utilize 3 "for" loops to make combinations among the 3 vectors ,
> which are (length)^3 in number , and find the one that maximize the
> likelihood ( maximum likelihood estimator).
> 
> 
> The code I created, runs but I think something goes wrong...because when I 
> change the length of the vectors but not the bounds the result is the
> same!!!
> 
> I will give a simple example(irrelevant but proportional to the above) to
> make it more clear...
> 
> Lets say we want to find the combination that maximize the multiplication of
> the entries of some vectors.
> 
> V1<-c(1,2,3)
> V2<-c(5, 2 , 4)
> V3<-c( 4, 3, 6)
> 
> The combination we look for is ( 3 , 5 , 6) that give us 3*5*6 = 90
> 
> If I apply the following in R , I won't take this result
> 
> V1<-c(1,2,3)
> V2<-c(5, 2 , 4)
> V3<-c( 4, 3, 6)
> 
> for( i in V1){
>  for( j in V2) {
> for( k in V3){
> 
> l<- i*j*k
> 
> }
> }
> }
> l
> 
> Then " l<- i*j*k " is  number and not vector(of all multiplications of all
> the combinations) , and is 3*4*6 = 72.
> 
> How can I fix the code?
> 
> 
> 
> 
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3992089.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> __
> R-help@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.

__
R-help@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.


Re: [R] nested "for" loops

2011-11-05 Thread Carl Witthoft
If in fact this is homework, you will do yourself, your classmates, and 
possibly your teacher if you let them know that, at least in R, almost 
anything you can do in a for() loop can be done easier and faster with 
vectorization.  If you teacher can't comprehend this, get him fired.


a<-c(4,6,3)
b<- c( 9,4,1)
d <- c(4,7,2,8)

winning.value <- max(outer(a,outer(b,d,"*"),"*"))



From: R. Michael Weylandt 
Date: Sat, 05 Nov 2011 10:21:05 -0400
Why do you need to do it with nested for loops? It is of course possible 
- and I hinted how to do it in my first email - but there's no reason as 
far as I can see to do so, particularly as a means of MLE. Sounds 
suspiciously like homework...


Michael

On Nov 4, 2011, at 10:14 PM, nick_pan  wrote:

> Thank you , this works but I have to do it with nested for loops...
>
> Could you suggest me a way ?
>
--

Sent from my Cray XK6
"Pendeo-navem mei anguillae plena est."

__
R-help@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.


Re: [R] nested "for" loops

2011-11-05 Thread R. Michael Weylandt
Why do you need to do it with nested for loops? It is of course possible - and 
I hinted how to do it in my first email - but there's no reason as far as I can 
see to do so, particularly as a means of MLE. Sounds suspiciously like 
homework...

Michael

On Nov 4, 2011, at 10:14 PM, nick_pan  wrote:

> Thank you , this works but I have to do it with nested for loops...
> 
> Could you suggest me a way ?
> 
> 
> 
> 
> 
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3992324.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> __
> R-help@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.

__
R-help@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.


Re: [R] nested "for" loops

2011-11-05 Thread nick_pan
Thank you , this works but I have to do it with nested for loops...

Could you suggest me a way ?





--
View this message in context: 
http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3992324.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] nested "for" loops

2011-11-04 Thread R. Michael Weylandt
Your problem is that you redefine l each time through the loops and
don't record old values; you could do so by using c() for
concatenation, but perhaps this is what you are looking for:

exp(rowSums(log(expand.grid(V1, V2, V3

Hope this helps,

Michael

On Fri, Nov 4, 2011 at 7:49 PM, nick_pan  wrote:
> Hi all , I have written a code with nested "for" loops .
> The aim is to estimate the maximum likelihood by creating 3 vectors with the
> same length( sequence )
> and then to utilize 3 "for" loops to make combinations among the 3 vectors ,
> which are (length)^3 in number , and find the one that maximize the
> likelihood ( maximum likelihood estimator).
>
>
> The code I created, runs but I think something goes wrong...because when I
> change the length of the vectors but not the bounds the result is the
> same!!!
>
> I will give a simple example(irrelevant but proportional to the above) to
> make it more clear...
>
> Lets say we want to find the combination that maximize the multiplication of
> the entries of some vectors.
>
> V1<-c(1,2,3)
> V2<-c(5, 2 , 4)
> V3<-c( 4, 3, 6)
>
> The combination we look for is ( 3 , 5 , 6) that give us 3*5*6 = 90
>
> If I apply the following in R , I won't take this result
>
> V1<-c(1,2,3)
> V2<-c(5, 2 , 4)
> V3<-c( 4, 3, 6)
>
> for( i in V1){
>  for( j in V2) {
>     for( k in V3){
>
> l<- i*j*k
>
> }
> }
> }
> l
>
> Then " l<- i*j*k " is  number and not vector(of all multiplications of all
> the combinations) , and is 3*4*6 = 72.
>
> How can I fix the code?
>
>
>
>
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3992089.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@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.
>

__
R-help@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.


[R] nested "for" loops

2011-11-04 Thread nick_pan
Hi all , I have written a code with nested "for" loops . 
The aim is to estimate the maximum likelihood by creating 3 vectors with the
same length( sequence ) 
and then to utilize 3 "for" loops to make combinations among the 3 vectors ,
which are (length)^3 in number , and find the one that maximize the
likelihood ( maximum likelihood estimator).


The code I created, runs but I think something goes wrong...because when I 
change the length of the vectors but not the bounds the result is the
same!!!

I will give a simple example(irrelevant but proportional to the above) to
make it more clear...

Lets say we want to find the combination that maximize the multiplication of
the entries of some vectors.

V1<-c(1,2,3)
V2<-c(5, 2 , 4)
V3<-c( 4, 3, 6)

The combination we look for is ( 3 , 5 , 6) that give us 3*5*6 = 90

If I apply the following in R , I won't take this result

V1<-c(1,2,3)
V2<-c(5, 2 , 4)
V3<-c( 4, 3, 6)

for( i in V1){
  for( j in V2) {
 for( k in V3){

l<- i*j*k

}
}
}
l

Then " l<- i*j*k " is  number and not vector(of all multiplications of all
the combinations) , and is 3*4*6 = 72.

How can I fix the code?




--
View this message in context: 
http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3992089.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] nested for loops

2010-07-05 Thread jim holtman
What do you want to do with the data being genereated?  In the loop
you have, it will just return the last value generated.  Let me ask my
favorite question: "What is the problem you are trying to solve".  If
you get a memory problem with expand.grid, then if you are trying to
store the values in the 'for' loop, you will have the same problem.
How big is 'n'?  If it is 3, you will have this many values:

> 3^20
[1] 3486784401

What are you going to do with them?

On Mon, Jul 5, 2010 at 5:06 PM, Senay ASMA  wrote:
> Dear Admin,
> I will appreciate if you advise me an effective way to write the following R
> code including nested for loops. I cannot do it by using expand.grid
> function because it results with memory allocation problems.
> Thanks for your time and consideration.
>
> for(d1 in 0:n){
> for(d2 in 0:n){
> for(d3 in 0:n){
> for(d4 in 0:n){
> for(d5 in 0:n){
> for(d6 in 0:n){
> for(d7 in 0:n){
> for(d8 in 0:n){
> for(d9 in 0:n){
> for(d10 in 0:n){
> for(d11 in 0:n){
> for(d12 in 0:n){
> for(d13 in 0:n){
> for(d14 in 0:n){
> for(d15 in 0:n){
> for(d16 in 0:n){
> for(d17 in 0:n){
> for(d18 in 0:n){
> for(d19 in 0:n){
> for(d20 in 0:n){
>
> list=c(d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20)
> 
>
>        [[alternative HTML version deleted]]
>
> __
> R-help@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
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
R-help@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.


Re: [R] nested for loops

2010-07-05 Thread Romain Francois


Le 05/07/10 23:06, Senay ASMA a écrit :


Dear Admin,
I will appreciate if you advise me an effective way to write the following R
code including nested for loops. I cannot do it by using expand.grid
function because it results with memory allocation problems.
Thanks for your time and consideration.

for(d1 in 0:n){
for(d2 in 0:n){
for(d3 in 0:n){
for(d4 in 0:n){
for(d5 in 0:n){
for(d6 in 0:n){
for(d7 in 0:n){
for(d8 in 0:n){
for(d9 in 0:n){
for(d10 in 0:n){
for(d11 in 0:n){
for(d12 in 0:n){
for(d13 in 0:n){
for(d14 in 0:n){
for(d15 in 0:n){
for(d16 in 0:n){
for(d17 in 0:n){
for(d18 in 0:n){
for(d19 in 0:n){
for(d20 in 0:n){

list=c(d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20)



Probably not what you want, but this should replicate the same effect as 
the code you posted:


list <- rep( n, 20 )

Romain

--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/98Uf7u : Rcpp 0.8.1
|- http://bit.ly/c6YnCi : graph gallery collage
`- http://bit.ly/bZ7ltC : inline 0.3.5

__
R-help@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.


[R] nested for loops

2010-07-05 Thread Senay ASMA
Dear Admin,
I will appreciate if you advise me an effective way to write the following R
code including nested for loops. I cannot do it by using expand.grid
function because it results with memory allocation problems.
Thanks for your time and consideration.

for(d1 in 0:n){
for(d2 in 0:n){
for(d3 in 0:n){
for(d4 in 0:n){
for(d5 in 0:n){
for(d6 in 0:n){
for(d7 in 0:n){
for(d8 in 0:n){
for(d9 in 0:n){
for(d10 in 0:n){
for(d11 in 0:n){
for(d12 in 0:n){
for(d13 in 0:n){
for(d14 in 0:n){
for(d15 in 0:n){
for(d16 in 0:n){
for(d17 in 0:n){
for(d18 in 0:n){
for(d19 in 0:n){
for(d20 in 0:n){

list=c(d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20)


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Nested For loops

2009-12-22 Thread Paul Hiemstra
Wouldn't the best approach be to adapt the acf function already 
available in R and change it only to suit your needs?


cheers,
Paul

baloo mia wrote:

In the ACF(nlme) the normalization of the numerator has been done by N and I 
want to normalize it by N-k, where N is the observations and k is the lag.

Baloo
--- On Tue, 12/22/09, ONKELINX, Thierry  wrote:

From: ONKELINX, Thierry 
Subject: RE: [R] Nested For loops
To: "baloo mia" , r-help@r-project.org
Date: Tuesday, December 22, 2009, 1:00 AM

Baloo,

Why don't you use the built-in acf function? 


Thierry



ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
thierry.onkel...@inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than 
asking him to perform a post-mortem examination: he may be able to say what the 
experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure 
that a reasonable answer can be extracted from a given body of data.
~ John Tukey
 
-Oorspronkelijk bericht-

Van: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Namens 
baloo mia
Verzonden: dinsdag 22 december 2009 2:07
Aan: r-help@r-project.org
Onderwerp: [R] Nested For loops

Dear R experts,

Might be very simple question to ask but would be insightful. As the same story of nested 
"for loops". following is the code that I am using to get the autocorrelation 
function of the sample data. I have tried to get rid of for loops but since I am touching 
R after such a long time that I need to practice more but I need help to revive my 
skills. I know that apply() or sapply() would be beneficial. I need help...

Best Wishes,
Baloo 


R code
--
acf_wal <- function (infile) {
data<-read.table(infile)
data_value <- data[,-1]
data_value_mean <- mean(data_value)
data_value_square <- (data_value - data_value_mean) ^ 2
square_sum<-sum(data_value_square)
entry<-NROW(data_value)
deno<-square_sum/entry

tab1<-c()
tab2<-c()
ps_value <- seq(0,(floor(entry/2)),1)

for(k in 0:(floor(entry/2))){

for (i in 1:(entry-k)) {
mult<-(data_value[i] - data_value_mean) * (data_value [i+k] - 
data_value_mean)
tab1 <- c(tab1,mult)
}
auto_avg<-mean(tab1)
tab1<-c()
auto_corr<-auto_avg/deno
tab2<-c(tab2,auto_corr)
}

table_value <- cbind (ps_value, tab2)
colnames(table_value) <- c("#ps", "acf")


outfile<-unlist(strsplit(infile, split=".", fixed=TRUE))[1]


write.table(table_value,file=paste(outfile,"acf.dat",sep="-"),row.names=FALSE,sep="\t",quote=F)
}

--

Sample data
--

1 16.0071
2 16.7966
3 17.575
4 18.1614
5 15.982
6 16.8515
7 15.6828
8 14.9652
9 14.8623
10 14.7079



Help in this regard would be highly appreciated.

 




  
[[alternative HTML version deleted]]



Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.




 
__

R-help@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.
  



--
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone:  +3130 274 3113 Mon-Tue
Phone:  +3130 253 5773 Wed-Fri
http://intamap.geo.uu.nl/~paul

__
R-help@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.


Re: [R] Nested For loops

2009-12-22 Thread baloo mia
In the ACF(nlme) the normalization of the numerator has been done by N and I 
want to normalize it by N-k, where N is the observations and k is the lag.

Baloo
--- On Tue, 12/22/09, ONKELINX, Thierry  wrote:

From: ONKELINX, Thierry 
Subject: RE: [R] Nested For loops
To: "baloo mia" , r-help@r-project.org
Date: Tuesday, December 22, 2009, 1:00 AM

Baloo,

Why don't you use the built-in acf function? 

Thierry



ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
thierry.onkel...@inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than 
asking him to perform a post-mortem examination: he may be able to say what the 
experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure 
that a reasonable answer can be extracted from a given body of data.
~ John Tukey
 
-Oorspronkelijk bericht-
Van: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Namens 
baloo mia
Verzonden: dinsdag 22 december 2009 2:07
Aan: r-help@r-project.org
Onderwerp: [R] Nested For loops

Dear R experts,

Might be very simple question to ask but would be insightful. As the same story 
of nested "for loops". following is the code that I am using to get the 
autocorrelation function of the sample data. I have tried to get rid of for 
loops but since I am touching R after such a long time that I need to practice 
more but I need help to revive my skills. I know that apply() or sapply() would 
be beneficial. I need help...

Best Wishes,
Baloo 

R code
--
acf_wal <- function (infile) {
    data<-read.table(infile)
    data_value <- data[,-1]
    data_value_mean <- mean(data_value)
    data_value_square <- (data_value - data_value_mean) ^ 2
    square_sum<-sum(data_value_square)
    entry<-NROW(data_value)
    deno<-square_sum/entry

    tab1<-c()
    tab2<-c()
    ps_value <- seq(0,(floor(entry/2)),1)
    
    for(k in 0:(floor(entry/2))){
        for (i in 1:(entry-k)) {
            mult<-(data_value[i] - data_value_mean) * (data_value [i+k] - 
data_value_mean)
            tab1 <- c(tab1,mult)
        }
            auto_avg<-mean(tab1)
            tab1<-c()
            auto_corr<-auto_avg/deno
            tab2<-c(tab2,auto_corr)
    }

    table_value <- cbind (ps_value, tab2)
    colnames(table_value) <- c("#ps", "acf")


    outfile<-unlist(strsplit(infile, split=".", fixed=TRUE))[1]

    
write.table(table_value,file=paste(outfile,"acf.dat",sep="-"),row.names=FALSE,sep="\t",quote=F)
}

--

Sample data
--

1 16.0071
2 16.7966
3 17.575
4 18.1614
5 15.982
6 16.8515
7 15.6828
8 14.9652
9 14.8623
10 14.7079



Help in this regard would be highly appreciated.

 



      
    [[alternative HTML version deleted]]


Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.



 
__
R-help@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.


Re: [R] Nested For loops

2009-12-22 Thread ONKELINX, Thierry
Baloo,

Why don't you use the built-in acf function? 

Thierry



ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
thierry.onkel...@inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than 
asking him to perform a post-mortem examination: he may be able to say what the 
experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure 
that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-Oorspronkelijk bericht-
Van: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Namens 
baloo mia
Verzonden: dinsdag 22 december 2009 2:07
Aan: r-help@r-project.org
Onderwerp: [R] Nested For loops

Dear R experts,

Might be very simple question to ask but would be insightful. As the same story 
of nested "for loops". following is the code that I am using to get the 
autocorrelation function of the sample data. I have tried to get rid of for 
loops but since I am touching R after such a long time that I need to practice 
more but I need help to revive my skills. I know that apply() or sapply() would 
be beneficial. I need help...

Best Wishes,
Baloo 

R code
--
acf_wal <- function (infile) {
    data<-read.table(infile)
    data_value <- data[,-1]
    data_value_mean <- mean(data_value)
    data_value_square <- (data_value - data_value_mean) ^ 2
    square_sum<-sum(data_value_square)
    entry<-NROW(data_value)
    deno<-square_sum/entry

    tab1<-c()
    tab2<-c()
    ps_value <- seq(0,(floor(entry/2)),1)
    
    for(k in 0:(floor(entry/2))){
        for (i in 1:(entry-k)) {
            mult<-(data_value[i] - data_value_mean) * (data_value [i+k] - 
data_value_mean)
            tab1 <- c(tab1,mult)
        }
            auto_avg<-mean(tab1)
            tab1<-c()
            auto_corr<-auto_avg/deno
            tab2<-c(tab2,auto_corr)
    }

    table_value <- cbind (ps_value, tab2)
    colnames(table_value) <- c("#ps", "acf")


    outfile<-unlist(strsplit(infile, split=".", fixed=TRUE))[1]

    
write.table(table_value,file=paste(outfile,"acf.dat",sep="-"),row.names=FALSE,sep="\t",quote=F)
}

--

Sample data
--

1 16.0071
2 16.7966
3 17.575
4 18.1614
5 15.982
6 16.8515
7 15.6828
8 14.9652
9 14.8623
10 14.7079



Help in this regard would be highly appreciated.

 



  
[[alternative HTML version deleted]]


Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.

__
R-help@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.


[R] Nested For loops

2009-12-21 Thread baloo mia
Dear R experts,

Might be very simple question to ask but would be insightful. As the same story 
of nested "for loops". following is the code that I am using to get the 
autocorrelation function of the sample data. I have tried to get rid of for 
loops but since I am touching R after such a long time that I need to practice 
more but I need help to revive my skills. I know that apply() or sapply() would 
be beneficial. I need help...

Best Wishes,
Baloo 

R code
--
acf_wal <- function (infile) {
    data<-read.table(infile)
    data_value <- data[,-1]
    data_value_mean <- mean(data_value)
    data_value_square <- (data_value - data_value_mean) ^ 2 
    square_sum<-sum(data_value_square)
    entry<-NROW(data_value)
    deno<-square_sum/entry

    tab1<-c()
    tab2<-c()
    ps_value <- seq(0,(floor(entry/2)),1)
    
    for(k in 0:(floor(entry/2))){
        for (i in 1:(entry-k)) {
            mult<-(data_value[i] - data_value_mean) * (data_value [i+k] - 
data_value_mean)
            tab1 <- c(tab1,mult)
        }
            auto_avg<-mean(tab1)
            tab1<-c()
            auto_corr<-auto_avg/deno
            tab2<-c(tab2,auto_corr)
    }

    table_value <- cbind (ps_value, tab2)
    colnames(table_value) <- c("#ps", "acf")


    outfile<-unlist(strsplit(infile, split=".", fixed=TRUE))[1]

    
write.table(table_value,file=paste(outfile,"acf.dat",sep="-"),row.names=FALSE,sep="\t",quote=F)
}

--

Sample data
--

1 16.0071
2 16.7966
3 17.575
4 18.1614
5 15.982
6 16.8515
7 15.6828
8 14.9652
9 14.8623
10 14.7079



Help in this regard would be highly appreciated.

 



  
[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Nested for loops

2009-07-14 Thread Michael Knudsen
On Tue, Jul 14, 2009 at 2:29 PM, Gabor
Grothendieck wrote:

> seq. <- function(from, to) seq(from = from, length = max(0, to - from + 1))

Really nice! Thank you!

-- 
Michael Knudsen
micknud...@gmail.com
http://lifeofknudsen.blogspot.com/

__
R-help@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.


Re: [R] Nested for loops

2009-07-14 Thread Michael Knudsen
On Tue, Jul 14, 2009 at 1:56 PM, David Winsemius wrote:

>> temp[ upper.tri(temp) ]
>  [1]  7 13 14 19 20 21 25 26 27 28 31 32 33 34 35

Thanks! I didn't know about that function; it certainly makes things a
lot easier. For example, until now I have used the following, homemade
expression

(1:N^2)[which((1:N^2)!=seq(0,(N-1)*N,by=N)+(1:N))]

to get the indices of the non-diagonal entries of a matrix :-)

-- 
Michael Knudsen
micknud...@gmail.com
http://lifeofknudsen.blogspot.com/

__
R-help@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.


Re: [R] Nested for loops

2009-07-14 Thread Gabor Grothendieck
Try this:

seq. <- function(from, to) seq(from = from, length = max(0, to - from + 1))

seq.(11, 10)



On Tue, Jul 14, 2009 at 1:38 AM, Michael Knudsen wrote:
> Hi,
>
> I have spent some time locating a quite subtle (at least in my
> opinion) bug in my code. I want two nested for loops traversing the
> above-diagonal part of a square matrix. In pseudo code it would
> something like
>
> for i = 1 to 10
> {
>   for j = i+1 to 10
>   {
>      // do something
>   }
> }
>
> However, trying to do the same in R, my first try was
>
> for (i in 1:10)
> {
>   for (j in (i+1):10)
>   {
>       // do something
>   }
> }
>
> but there's a problem here. For i=10, the last for loop is over 11:10.
> Usually programming laguages would regard what corresponds to 11:10 as
> empty, but A:B with A bigger than B is in R interpreted as the numbers
> from B to A in reverse order.
>
> Is there a clever way to make nested loops like the one above in R?
>
> --
> Michael Knudsen
> micknud...@gmail.com
> http://lifeofknudsen.blogspot.com/
>
> __
> R-help@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.
>

__
R-help@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.


Re: [R] Nested for loops

2009-07-14 Thread David Winsemius


On Jul 14, 2009, at 2:25 AM, Michael Knudsen wrote:

On Tue, Jul 14, 2009 at 8:20 AM, Michael  
Knudsen wrote:



What do you mean? It looks a like a very general solution to me.


Just got an email suggesting using the functions col and row. For  
example


temp = matrix(c(1:36),nrow=6)
which(col(temp)>row(temp))




This gives the indices (in the matrix viewed as a vector) of the
above-diagonal entries.



If you want the entries it would then be:
temp[ col(temp) > row(temp) ]

But more simply:

> temp[ upper.tri(temp) ]
 [1]  7 13 14 19 20 21 25 26 27 28 31 32 33 34 35
>

If you want the row and column numbers then:
> row(temp)[ col(temp) > row(temp)]
 [1] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
> col(temp)[ col(temp) > row(temp) ]
 [1] 2 3 3 4 4 4 5 5 5 5 6 6 6 6 6

Any solution that uses col and row will create two additional matrices  
of the same size as the original at least for the duration of the  
operation.


For the sub-and super-diagonals:

http://finzi.psych.upenn.edu/Rhelp08/2009-March/191379.html
--

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
R-help@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.


Re: [R] Nested for loops

2009-07-13 Thread Michael Knudsen
On Tue, Jul 14, 2009 at 8:20 AM, Michael Knudsen wrote:

> What do you mean? It looks a like a very general solution to me.

Just got an email suggesting using the functions col and row. For example

temp = matrix(c(1:36),nrow=6)
which(col(temp)>row(temp))

This gives the indices (in the matrix viewed as a vector) of the
above-diagonal entries.

-- 
Michael Knudsen
micknud...@gmail.com
http://lifeofknudsen.blogspot.com/

__
R-help@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.


Re: [R] Nested for loops

2009-07-13 Thread Michael Knudsen
On Tue, Jul 14, 2009 at 8:03 AM, Moshe Olshansky wrote:

> Make it
> for (i in 1:9)

Thanks. That's also how I solved the problem myself. I just somehow
think it makes my code look rather clumsy and opaque. Maybe I just
have to get used to this kind of nasty tricks.

> This is not the general solution, (...)

What do you mean? It looks a like a very general solution to me.

-- 
Michael Knudsen
micknud...@gmail.com
http://lifeofknudsen.blogspot.com/

__
R-help@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.


Re: [R] Nested for loops

2009-07-13 Thread Moshe Olshansky

Make it
for (i in 1:9)

This is not the general solution, but in your case when i=10 you do not want to 
do anything.

--- On Tue, 14/7/09, Michael Knudsen  wrote:

> From: Michael Knudsen 
> Subject: [R] Nested for loops
> To: r-help@r-project.org
> Received: Tuesday, 14 July, 2009, 3:38 PM
> Hi,
> 
> I have spent some time locating a quite subtle (at least in
> my
> opinion) bug in my code. I want two nested for loops
> traversing the
> above-diagonal part of a square matrix. In pseudo code it
> would
> something like
> 
> for i = 1 to 10
> {
>    for j = i+1 to 10
>    {
>       // do something
>    }
> }
> 
> However, trying to do the same in R, my first try was
> 
> for (i in 1:10)
> {
>    for (j in (i+1):10)
>    {
>        // do something
>    }
> }
> 
> but there's a problem here. For i=10, the last for loop is
> over 11:10.
> Usually programming laguages would regard what corresponds
> to 11:10 as
> empty, but A:B with A bigger than B is in R interpreted as
> the numbers
> from B to A in reverse order.
> 
> Is there a clever way to make nested loops like the one
> above in R?
> 
> -- 
> Michael Knudsen
> micknud...@gmail.com
> http://lifeofknudsen.blogspot.com/
> 
> __
> R-help@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.
>

__
R-help@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.


Re: [R] Nested for loops

2009-07-13 Thread Daniel Nordlund
> -Original Message-
> From: r-help-boun...@r-project.org 
> [mailto:r-help-boun...@r-project.org] On Behalf Of Michael Knudsen
> Sent: Monday, July 13, 2009 10:39 PM
> To: r-help@r-project.org
> Subject: [R] Nested for loops
> 
> Hi,
> 
> I have spent some time locating a quite subtle (at least in my
> opinion) bug in my code. I want two nested for loops traversing the
> above-diagonal part of a square matrix. In pseudo code it would
> something like
> 
> for i = 1 to 10
> {
>for j = i+1 to 10
>{
>   // do something
>}
> }
> 
> However, trying to do the same in R, my first try was
> 
> for (i in 1:10)
> {
>for (j in (i+1):10)
>{
>// do something
>}
> }
> 
> but there's a problem here. For i=10, the last for loop is over 11:10.
> Usually programming laguages would regard what corresponds to 11:10 as
> empty, but A:B with A bigger than B is in R interpreted as the numbers
> from B to A in reverse order.
> 
> Is there a clever way to make nested loops like the one above in R?
> 

Michael,

If you are tryuly interested in the ABOVE diagonal elements (i.e. not
including diagonal element temselves), the your loop should be

 for (i in 1:9)
 {
for (j in (i+1):10)
{
// do something
}
 }

And you avoid your current problem.  

Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA

__
R-help@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.


[R] Nested for loops

2009-07-13 Thread Michael Knudsen
Hi,

I have spent some time locating a quite subtle (at least in my
opinion) bug in my code. I want two nested for loops traversing the
above-diagonal part of a square matrix. In pseudo code it would
something like

for i = 1 to 10
{
   for j = i+1 to 10
   {
  // do something
   }
}

However, trying to do the same in R, my first try was

for (i in 1:10)
{
   for (j in (i+1):10)
   {
   // do something
   }
}

but there's a problem here. For i=10, the last for loop is over 11:10.
Usually programming laguages would regard what corresponds to 11:10 as
empty, but A:B with A bigger than B is in R interpreted as the numbers
from B to A in reverse order.

Is there a clever way to make nested loops like the one above in R?

-- 
Michael Knudsen
micknud...@gmail.com
http://lifeofknudsen.blogspot.com/

__
R-help@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.