################################################################################ # # Codewalk Base R ################################################################################ ## FUNCTIONS # no default value # function will return the value for the last executed line addNumber <- function (x, y) { x+y } addNumber(4, 2) addNumber(4) # you can add a default argument value addNumber <- function (x, y=10) { x+y } addNumber(4) # with return fucntion addNumber <- function (x, y=10) { z <- x+y #return(z) } addNumber(4) # be aware when R providesa simple vectorization solution # do not provide a loop solution x <- c( 3, 4, 5) y <- x + 1 # simple solution # never this y <- c() for (e in x) { y <- c(y, e + 1) } ################################################################################ ## BUILT IN HELPER FUNCTIONS args(which) ?which help(which) ################################################################################ ## COMMENT AND UNCOMMENT # highlight block of code then press ctrl+shift+c or cmd+shift+c in Mac # Peter Piper picked a peck of pickled peppers. # A peck of pickled peppers Peter Piper picked. # If Peter Piper picked a peck of pickled peppers, # Where's the peck of pickled peppers Peter Piper picked? ################################################################################ ## MISSING VALUES: NaN vs. NA # NaN values are NA, but NA values are not NaN # division by zero results in Inf NOT NaN a <- c(0/0, 1, 2/0, 3, NA) a is.na(a) is.nan(a) # demonstrates that Inf is a number a <- na.omit(a) a ############################################### ## CONDITIONAL FLOW CONTROL # hours range from 0-23 thisHour <- as.POSIXlt(Sys.time())$hour ?POSIXlt if (thisHour<12) { print("It's morning") } else if (thisHour==12) { print("It's noon time") } else if (thisHour>12 & thisHour<5) { print("It's afternoon") } else { print("It's evening") } # ifelse() function is like the ternary operator # weekdays range from 0-6 today <- as.POSIXlt(Sys.time())$wday ifelse( today==0 & today==6, "It's a weekend", "It's a weekday") ######################################################## # LOOPS WITH "apply" FAMILY OF FUNCTIONS # apply() # applies function on margins of arrays or matrices and returns a vector or array # an array in R is a vector with the additional attributes dim() and dimnames() # 2D array is a matrix # MARGIN = 1 apply over rows, 2 appply over columns, c(1,2) apply over both x <- matrix(seq(12), nrow=3, ncol=4) x sum_rows <- apply(X=x, MARGIN=1, FUN=sum) sum_rows is.vector(sum_rows) sum_cols <- apply(X=x, MARGIN=2, FUN=sum) sum_cols # lapply() # applies a function over each item in a list and returns a list # x will be coerced into a list type if it isn't already one. # lapply(X=, FUN=, ... ) ... optional arguments to the function lapply(1:5, function(x) x^3 + 9) unlist(lapply(1:5, function(x) x^3 + 9)) # sapply() # similar to lapply() but returns a vector instead of a list sapply(1:5, function(x) x^3 + 9) # More advanced apply functions not covered: tapply() and mapply(). # If you are interested, explore those on your own.