r: functions and avoiding loops

April 08, 2015

After getting started with the basic syntax and some frequently used functions, I needed to learn to write some code.

I enjoyed this nice intro to functions.


myfunction <- function(arg1, arg2, ... ) {
   statements
   return(object)
}

We can put functions (or any code) in a text file and load it from the R command line:


source("mycode.R")

where mycode.R is in the same directory as where I’m running R or a full path to the script.

But then I got stuck in how to transform my data table. As much as I wanted to iterate using loops, I felt that was a very un-R-like solution. I found some good patterns in @xieyihui‘s gory loops post.

If you need to make a string, you can use paste like Ruby’s join method:


> words  words
[1] "one"   "two"   "three"

> paste(words)
[1] "one"   "two"   "three"

> paste(words, collapse="")
[1] "onetwothree"

> paste(words, collapse=",")
[1] "one,two,three"

> paste(words, collapse=", ")
[1] "one, two, three"