0% found this document useful (0 votes)
18 views19 pages

For - Loops in R

The document discusses for loops in R including basic for loop syntax and examples of using for loops to iterate over vectors and matrices, combining for loops with if/else statements, and using next and break inside for loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views19 pages

For - Loops in R

The document discusses for loops in R including basic for loop syntax and examples of using for loops to iterate over vectors and matrices, combining for loops with if/else statements, and using next and break inside for loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Loops in R

Raju N
For loop
• for(variable in vector) {
Commands
}
For loop examples
• > for(i in 1:5)
• + { print(i^2)
• +}
• [1] 1
• [1] 4
• [1] 9
• [1] 16
• [1] 25
• >
Examples contd…
• Indices need not be sequential...
• > for(i in c(-5,-3,0,1,2,3,4))
• + {print(i^2)
• +}
• [1] 25
• [1] 9
• [1] 0
• [1] 1
• [1] 4
• [1] 9
• [1] 16
Another way…
• > x= c(-5,-3,0,1,2,3,4)
• > for(i in x){print (i^2)}
• [1] 25
• [1] 9
• [1] 0
• [1] 1
• [1] 4
• [1] 9
• [1] 16
Example…
• > for(i in c(-5,-3,0,1,2,3,4)){print (c(i, i^2))}
• [1] -5 25
• [1] -3 9
• [1] 0 0
• [1] 1 1
• [1] 2 4
• [1] 3 9
• [1] 4 16
Example
• > data=numeric(5)
• > data
• [1] 0 0 0 0 0
• > for(i in 1:5){data[i]=i^2}
• > data
• [1] 1 4 9 16 25
Example…
• > x= c(-5,-3,0,1,2)
• > data1=numeric(5)
• > for(i in 1:5){data1[i]=x[i]^2}
• > data1
• [1] 25 9 0 1 4
Length in meter to length in cm
• > for(lenm in c(3,2.75,.9))
• + {lenincm=lenm*100
• + print(c(lenm,lenincm))}
• [1] 3 300
• [1] 2.75 275.00
• [1] 0.9 90.0
For and If combined
• > for(ht in c(120,130,175,160,185,190,110))
• + { if (ht >= 170){print(c(ht,"TALL"))}
• + else{print(c(ht,"SHORT"))}}
• [1] "120" "SHORT"
• [1] "130" "SHORT"
• [1] "175" "TALL"
• [1] "160" "SHORT"
• [1] "185" "TALL"
• [1] "190" "TALL"
• [1] "110" "SHORT"
Without for
• > x=1:5
• > y=x^2
• >y
• [1] 1 4 9 16 25
Nested For loops
• for(var1 in vect1){
• for(var2 in vect2){
• for(var3 in vect3){……{ for(varn in vect3)
• {
• Commands
• }}}}}

• Note that the number of opening brackets should be


equal to umber of closing brackets.
Example
• > data=matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=3,byrow=T)
• > data
• [,1] [,2] [,3] [,4]
• [1,] 1 2 3 4
• [2,] 5 6 7 8
• [3,] 9 10 11 12
• > data1=data
• > for(i in 1:3){for(j in 1:4){data[i,j]=data1[i,j]+data[i,j]
• + }}
• > data
• [,1] [,2] [,3] [,4]
• [1,] 2 4 6 8
• [2,] 10 12 14 16
• [3,] 18 20 22 24
For…
• > sum=1
• > for(i in 2:10)
• + { sum=sum*i}
• > sum
• [1] 3628800
• > 1*2*3*4*5*6*7*8*9*10
• [1] 3628800
For…
• > sum=1
• > for(i in seq(2,10,2))
• + { sum=sum*i}
• > sum
• [1] 3840
• > 2*4*6*8*10
• [1] 3840
For…
• > sum=1
• > for(i in 2:10)
• + { if (i%%2 ==0){ sum=sum*i}}
• > sum
• [1] 3840
Next statement
• > sum=1
• > for(i in 1:10)
• + { if (i==6) {next
• +}
• + sum=sum * i
• +}
• > sum
• [1] 604800
• > 1*2*3*4*5*7*8*9*10
• [1] 604800
Break statement
• > sum=1
• > for(i in 1:10)
• + { sum=sum*i
• +{
• + if (i==6)
• + {break
• +}
• +}
• +}
• > sum
• [1] 720
• > 1*2*3*4*5*6
• [1] 720
For loop
• > for(i in letters)
• + {print(i,)}
• [1] "a"
• [1] "b"
• [1] "c"
• [1] "d"
• [1] "e"
• [1] "f"
• [1] "g"
• [1] "h"
• [1] "i"
• [1] "j"

You might also like