(x=LETTERS[1:5]) for (i in x) print(i) (x=rep("A",4)) #elements of vector do not need to be different for (i in x) print(i) for (i in c(6,6,6,6) ) print(i) #even if they are numbers for (i in c(6,6,6,6) ) i # need print() within loops or functions (x=letters[1:5]) for (i in 1:length(x)) # if you need the counter i as an integer cat("i is",i,"and x[",i,"] is",x[i],"\n") # use for (i in 1:n) # note the unwanted spaces, which you can get rid of with sep="", like in paste() for (i in 1:length(x)) # but then you have to add spaces in manually elsewhere cat("i is ",i," and x[",i,"] is ",x[i],"\n",sep="") # alternatively for (i in 1:length(x)) print(paste0("i is ",i," and x[",i,"] is ",x[i])) #which is not as pretty as cat due to [1] and " " # but cat() cannot print lists Lst=list(name="Fred",wife="Mary",no.children=3,ages=c(4,7,9)) cat(Lst) #error print(Lst) # but print can. # print() takes only one argument, but it can be complicated # cat() can take many arguments, but they must all be simple #Matloff advocates on page 34 for (i in seq(x)) cat("i is",i,"and x[i] is",x[i],"\n") # to avoid x=NULL for (i in 1:length(x)) cat("i is",i,"and x[i] is",x[i],"\n") for (i in seq(x)) cat("i is",i,"and x[i] is",x[i],"\n") x=3 # but then scalars are a problem for (i in 1:length(x)) cat("i is",i,"and x[i] is",x[i],"\n") for (i in seq(x)) cat("i is",i,"and x[i] is",x[i],"\n") # so we really need along=x as in Ligges pg 53 to get it perfectly right for (i in seq(along=x)) cat("i is",i,"and x[i] is",x[i],"\n") # My clumsier solution in the past has been n=length(x) if (n>0) for (i in 1:n) cat("i is",i,"and x is",x[i],"\n") if (TRUE) 3 else 1 # if else returns values (if (TRUE) y=3 else y=1) # even if they are assigned if (TRUE) y=3 else y=1 # even if they are assigned (if (0) y=3 else y=1) # 0 is FALSE (if (1) y=3 else y=1) # 1 is TRUE (if (10) y=3 else y=1) # as is any number not equal to zero x=3 (y <- if (x==2) x else x+1) # less typing if (x==2) y <- x else y <- x+1 # easier to read y # note that this throws an error when running line by line code if (x==2) y <- x # so alway try to bring the else up to the if line else y <- x+1 if (x==2) y <- x else # i.e. like this y <- x+1 i=1 while(i<=100){ print(i) i=i+1 } i=1 while(1){ print(i) if (i==100) break i=i+1 } i=1 repeat{ print(i) if (i==100) break i=i+1 } # i.e. repeat is the same as while(1) so I never use repeat. # Use next instead of break if you just want to skip the rest of the # current iteration, but then make sure something else ends the loop i=0 while(1){ i=i+1 if (i%%2==0) next #skip evens print(i) if (i==9) break } # Loops are slow. Use vector math whenever possible since this lets things # get done in lower level C code. # if you get stuck in an infinite loop, use esc to get out while(1){ print(i) i=i+1 } # if else statements fork into two branches # switch forks the flow into n branches # the first argument controls the output channel switch(2,a=11,b=12,c=13) switch("c",a=11,b=12,c=13) (x=switch("d",a=11,b=12,c=13) ) # NULL returned if (NA) print("hi") if (NaN) print("hi") if (1) print("hi") if (0) print("hi") if (0.1) print("hi") if (-0.3) print("hi") if (-Inf) print("hi") if (Inf) print("hi")