Difference between revisions of "Loop.R"

From Organic Design wiki
(Naming the loop)
m
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
# {{R}}
 
# ==============================================================================
 
# ==============================================================================
 
# Loop evaluation e.g. 6 parts, 4 comparisons
 
# Loop evaluation e.g. 6 parts, 4 comparisons
Line 5: Line 6:
 
# e.g. 1,2,3,4 2,3,4,5 3,4,5,6 etc
 
# e.g. 1,2,3,4 2,3,4,5 3,4,5,6 etc
 
# ==============================================================================
 
# ==============================================================================
 +
 +
 +
loopName <- c(0,14,25, 35,60,87,132,146)
  
 
start  <- 1
 
start  <- 1
loop    <- 6
+
loop    <- length(loopName)
 
comp    <- 4
 
comp    <- 4
 
loopId <- 0:(loop+comp-1) %% loop +1
 
loopId <- 0:(loop+comp-1) %% loop +1
 
loopName <- c(0,14,25, 35,60,87,132,146)
 
  
 
for(i in seq(loop)){
 
for(i in seq(loop)){
   print(loopName[loops[seq(from=i, length=comp)]])
+
   print(loopName[loopId][seq(from=i, length=comp)])
 
}
 
}
  
Line 21: Line 23:
 
# 14 25 35 60
 
# 14 25 35 60
 
# 25 35 60 87
 
# 25 35 60 87
# 35 60 87  0
+
# 35 60 87 132
# 60 87 0 14
+
# 60 87 132 146
# 87 0 14 25
+
# 87 132 146  0
 +
# 132 146  0 14
 +
# 146  0 14 25

Latest revision as of 00:40, 6 June 2007

Code snipits and programs written in R, S or S-PLUS

  1. ==============================================================================
  2. Loop evaluation e.g. 6 parts, 4 comparisons
  3. this function is construting an array e.g. 1,2,3,4,5,6,1,2,3
  4. and then subsetting sequential arrays of length four from it
  5. e.g. 1,2,3,4 2,3,4,5 3,4,5,6 etc
  6. ==============================================================================


loopName <- c(0,14,25, 35,60,87,132,146)

start <- 1 loop <- length(loopName) comp <- 4 loopId <- 0:(loop+comp-1) %% loop +1

for(i in seq(loop)){

 print(loopName[loopId][seq(from=i, length=comp)])

}

  1. Returns;
  2. 0 14 25 35
  3. 14 25 35 60
  4. 25 35 60 87
  5. 35 60 87 132
  6. 60 87 132 146
  7. 87 132 146 0
  8. 132 146 0 14
  9. 146 0 14 25