ABSTRACT

R is not just a data analysis environment but a programming language. This chapter introduces three key programming concepts: conditional expressions, for-loops, and functions. These are not just key building blocks for advanced programming, but are sometimes useful during data analysis. Conditional expressions are one of the basic features of programming. They are used for what is called flow control. The most common conditional expression is the if-else statement. In R, we can actually perform quite a bit of data analysis without conditionals. For-loops are an important concept to understand, in R we rarely use them. A vectorized function is a function that will apply the same operation on each of the vectors. The chapter covers the functional that operates on numeric, logical, and character vectors: sapply. The function sapply permits to perform element-wise operations on any function. Other functionals are apply, lapply, tapply, mapply, vapply, and replicate.