################################################################################ ## 6. CONTROL WORKFLOW and CUSTOM FUNCTIONS ################################################################################ Shop = read.table("http://edu.sablab.net/data/txt/shop.txt",header=T,sep="\t") a=1 b=2 ##------------------------------ ## IF condition if (a==b) { print("a equals to b") } else { print("a is not equal to b") } ## use if in-a-line ifelse(a>b, a, b) ##------------------------------ ## FOR loop ## print all information for the first client for (i in 1:ncol(Shop)) print(Shop[1,i]) ##------------------------------ ## WHILE loop ## print all information for the first client i=1; while (i <= ncol(Shop)){ print(Shop[1,i]) i=i+1 } ##------------------------------ ## REPEAT loop i=1 repeat { print(i) i=i+1 if (i>10) break } ## "break" and "next" - help to control flow ##------------------------------ ## Custom functions ##------------------------------ ## Let us write a function to print vectors printVector = function(x, name=""){ print(paste("Vector",name,"with",length(x),"elements:")) if (length(x)>0) for (i in 1:length(x)) print(paste(name,"[",i,"] =",as.character(x[i]))) } printVector(Shop$Payment, "Payment") ##---------------------------------- ## Run script, saved in other files ##---------------------------------- source("http://sablab.net/scripts/LibMA.r") ls()