Useful R snippets


on flickr by user zzpza Jan 2012, THX!In this post we collect several R one- or few-liners that we consider useful. As our minds tend to forget these little fragments we jot them down here so we will find them again.

Subsequently re-calling a function that takes two arguments

Suppose we wanted to call a function that takes two arguments and use the results as a argument to the same function again. For example may want to sum up the values 1 to 5 Of course the function sum will do this for us, but what if this function didn’t exist? We might of course write:

1 + 2 + 3 + 4 + 5

But how do that in a single function call? Using or the like will not work, as the function "+" takes two arguments."+", list(1:5))

The trick is to use the function Reduce.

Reduce("+", 1:5)
> 15

Evaluating an R command stored in a character string

From time to time, you may encounter situations where you have to evaluate a command which is stored in a character string. For example, let’s assume that we have the following variables:

name1 <- "Steve"
name2 <- "Bill"
value1 <- 1
value2 <- 0

Now, what would you do if you have to create a vector with entries whose value is stored in the variables value1 and value2 and entry names whose value is stored in the variables name1 and name2? You can write:

command <- paste("values=c(",name1,"=",value1,",",
values <- eval(parse(text=command))

After issuing those command a vector named values is going to be created with named entries and values as follows

Steve  Bill
1     0

Creating an empty dataframe with zero rows

Sometimes I want to fill up a dataframe from the frist row on. It might be useful do start off with a dataframe with zero rows for that purpose. The function numeric or character do the job. In case we wanted to specify a factor with predefined levels also factor may be useful.

data.frame(a=numeric(), b=numeric())
data.frame(a=numeric(), b=character(), c=factor(levels=1:10), stringsAsFactors=F)

… to be continued.

Tamas and Mark

About these ads

5 Responses to “Useful R snippets”

  1. Could you clarify the one where you talk about the append function? It looks like a typo

  2. 2 markheckmann

    I wrote this post a while ago and it was only published now. You are right, something is wrong there. Also I do not see the point of using append() anymore as c() will do the job, i.e. someting like res <- c(res, paste(i,j))
    As I cannot really remember the purpose of the part, so I erased it.
    Thanks for pointing it out.

  3. 3 nattomi

    UPDATE! the second section of the post was gibberish – probably due to a formatting error – now I corrected it in a reproducible way.

  4. 4 Sascha W.

    Great, great blog! I just found you guys…

    In case you want to include another snippet function:

    I once wrote a really short function for shifting vectors. It produces a vector of the same length as vec with NAs filled in where the vector is “shifted out of bounds”. The parameter “shift” is a numeric with the direction (> 0: right; < 0: left) and distance of shift (absolute value). nothing sophisticated, but quite useful sometimes…

    # shift a vector (argument 'vec') to the left ('shift' 0)

    shift.vec = 0) {
    c(rep(NA, shift), vec[1:(length(vec)-shift)]) }
    else {
    c(vec[(abs(shift)+1):length(vec)], rep(NA, abs(shift))) } }

  5. 5 Sascha W.

    Oh, and a short comment to your “” post: This command is also very useful if applied to a list of dataframes (maybe the result of splitting a dataframe) and the “rbind” command.

    new.df <-"rbind", list.with.splitted.dfs)

    Everytime you want to iterate over a list of dataframes and manipulating every dataframe in a specific way, this command is MUCH faster than subsequently building up a new dataframe with the "rbind" command while iterating over the list.


Get every new post delivered to your Inbox.

Join 47 other followers

%d bloggers like this: