#switch centre <- function(x, type) { switch(type, mean = mean(x), median = median(x), trimmed = mean(x, trim = .1)) } #ifelse example.ifelse=function(x){ z=ifelse(x >= 0, sqrt(x), NA) return(z) } #break example.break=function(x){ for(i in 1:x){ print(i) if(i>5){ break } } } example.break2=function(A,B){ for (i in 1:A){ for (j in 1:B){ if (j==3) { break } else { cat (i,j,"\n") } } } } example.next=function(A,B){ for (i in 1:A){ for (j in 1:B){ if (j==3) { next } else { cat (i,j,"\n") } } } } ### #stop # write a function which stops and gives a warning when argument is smaller then 0. Sqrt=function(x){ if(x<0) stop("Argument should be >= 0") else sqrt(x) } }