There are three different loop structures in R:
1.repeat structure
It simply repeats the same expression: repeat expression, if you want to jump out of a loop, you can use the break command to jump to the next iteration in the loop, you need to use the next command, and if the break command is not included in the loop, the R code will be an infinite loop.
Therefore, the commonly used structure is as follows:
Repeat { expression if (condition) { break }}
Examples are as follows:
#求 1-100 and
I <-1sum_100 <-0repeat{sum_100 = sum_100 + I i=i+1 if (i>100) { print (sum_100) break }}
2.while structure: Repeats a particular expression when a condition is true
while (condition) expression
Examples are as follows:
> I <-1> sum_100 <-0> while (i<=100) {sum_100=sum_100+i;i=i+1}> print (sum_100) [1] 5050
3. The For loop structure can traverse the vector or each element in the list:
for (var in list) expression
Examples are as follows:
> for (i in SEQ (from=1,to=100,by=1)) sum_100=sum_100+i> Sum_100[1] 5050
4. Cyclic expansion
We all know that in modern programming languages like C # or Java, there will be iterators like foreach. The R language itself does not provide such a mechanism, but we can add packages to the R language to implement
(1) Iterator: An abstract object that returns an element from another object. Using iterators makes your code more readable and easy to execute in parallel. Adding the R language Extension pack iterators can implement the iterator functionality. Iterators can return elements of vectors, arrays, data frames, or other objects, and can return functions as well.
ITER (Obj,checkfunc=function (...) True,recycle=false,...) # OBJ Specifies the object # CHECKFUNC specifies a function that filters the return value of the iterator # RECYLE specifies whether to reset the iteration after the object element iteration is complete
Examples are as follows:
> Iter_one <-iter (1:10,checkfunc=function (x) x%%2==0,recycle=f) > Nextelem (iter_one) #nextElem () The function is used to view the next iteration, which implicitly calls Checkfunc and returns the value if the next value conforms to Checkfunc, otherwise iterates over the next value until either a value that matches checkfunc is found or all values are iterated [1] 2> Nextelem (Iter_one) [1] 4> Nextelem (Iter_one) [1] 6> Nextelem (Iter_one) [1] 8> Nextelem (Iter_one) [1] 10> Nextelem (iter_one) error:stopiteration #由于设定了recycle =f (also the default), when the iteration is complete, calling the Nextelem () function again will cause error: Stopiteration. If the recycle=t is set, the iteration will reset and output again 2,4,6,8,10
(2) foreach Loop: This function is implemented through the R language extension package foreach , which loops through multiple elements in an object (vector, matrix, data frame, or iterator), executes an expression against each element, and returns the result.
foreach (...,. Combine,. Init,. Final=null,. Inorder=true, . Multicombine=false,. maxcombine=if (. Multicombine) 2 Else, . Errorhandling=c (' Stop ', ' remove ', ' pass '), . Packages=null,. Export=null,. Noexport =null, . Verbose=false)
To actually execute a foreach loop, you need to use the %do% or %dopar% operator
> I_square <-foreach (i=1:5)%do% i^2> i_square[[1]][1] 1[[2]][1] 4[[3]][1] 9[[4]][1] 16[[5]][1] 25
The%do% operator executes the expression sequentially, and the%dopar% operator can be used to execute the expression in parallel.
Loops in the R language and their extensions: ITER and foreach