(This article was first published on R-posts.com, and kindly contributed to R-bloggers)
I guess I wrote my R “hello world!” function 7 or 8 years ago while approaching R for the first time. And it is too little to illustrate the basic syntax of a programming language for a working program to a wannabe R programmer. Thus, here follows a collection of basic functions that may help a bit more than the famed piece of code.
##################################################################### Hello world functions ######################################################################################################### General infofun <- function( arguments ) { body }##################################foo.add <- function(x,y){ x+y}foo.add(7, 5)----------------------------------foo.above <- function(x){ x[x>10]}foo.above(1:100)----------------------------------foo.above_n <- function(x,n){ x[x>n]}foo.above_n(1:20, 12)----------------------------------foo = seq(1, 100, by=2)foo.squared = NULLfor (i in1:50 ) { foo.squared[i] = foo[i]^2}foo.squared----------------------------------a <- c(1,6,7,8,8,9,2)s <- 0for (i in1:length(a)){ s <- s + a[[i]]}s----------------------------------a <- c(1,6,7,8,8,9,2,100)s <- 0i <- 1while (i <= length(a)){ s <- s + a[[i]] i <- i+1}s----------------------------------FunSum <- function(a){ s <- 0 i <- 1 while (i <= length(a)){ s <- s + a[[i]] i <- i+1 } print(s)}FunSum(a)-----------------------------------SumInt <- function(n){ s <- 0 for (i in1:n){ s <- s + i } print(s) }SumInt(14)-----------------------------------# find the maximum# right to left assignmentx <- c(3, 9, 7, 2)# trick: it is necessary to use a temporary variable to allow the comparison by pairs of# each number of the sequence, i.e. the process of comparison is incremental: each time# a bigger number compared to the previous in the sequence is found, it is assigned as the# temporary maximum# Since the process has to start somewhere, the first (temporary) maximum is assigned to be# the first number of the sequencemax <- x[1]for(i in x){ tmpmax = i if(tmpmax > max){ max = tmpmax }}maxx <- c(-20, -14, 6, 2)x <- c(-2, -24, -14, -7)min <- x[1]for(i in x){ tmpmin = i if(tmpmin < min){ min = tmpmin }}min----------------------------------# n is the nth Fibonacci number# temp is the temporary variableFibonacci <- function(n){ a <- 0 b <- 1 for(i in1:n){ temp <- b b <- a a <- a + temp } return(a)}Fibonacci(13)----------------------------------# R available factorial functionfactorial(5)# recursive function: ffff <- function(x) { if(x<=0) { return(1) } else { return(x*ff(x-1)) # function uses the fact it knows its own name to call itself }}ff(5)----------------------------------say_hello_to <- function(name){ paste("Hello", name)} say_hello_to("Roberto")----------------------------------foo.colmean <- function(y){ nc <- ncol(y) means <- numeric(nc) for(i in1:nc){ means[i] <- mean(y[,i]) } means}foo.colmean(airquality)----------------------------------foo.colmean <- function(y, removeNA=FALSE){ nc <- ncol(y) means <- numeric(nc) for(i in1:nc){ means[i] <- mean(y[,i], na.rm=removeNA) } means}foo.colmean(airquality, TRUE)----------------------------------foo.contingency <- function(x,y){ nc <- ncol(x) out <- list() for (i in1:nc){ out[[i]] <- table(y, x[,i]) } names(out) <- names(x) out}set.seed(123)v1 <- sample(c(rep("a", 5), rep("b", 15), rep("c", 20)))v2 <- sample(c(rep("d", 15), rep("e", 20), rep("f", 5)))v3 <- sample(c(rep("g", 10), rep("h", 10), rep("k", 20)))data <- data.frame(v1, v2, v3)foo.contingency(data,v3)
That’s all folks!
#R #rstats #maRche #Rbloggers
This post is also shared in LinkedIn and www.r-bloggers.com
var vglnk = { key: '949efb41171ac6ec1bf7f206d57e90b8' }; (function(d, t) {var s = d.createElement(t); s.type = 'text/javascript'; s.async = true;s.src = '//cdn.viglink.com/api/vglnk.js';var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r); }(document, 'script'));To leave a comment for the author, please follow the link and comment on their blog: R-posts.com.
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...