Useful R snippets
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 do.call or the like will not work, as the function "+" takes two arguments.
do.call("+", 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,",", name2,"=",value2,")",sep="") 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
Filed under: R / R-Code | 5 Comments
Could you clarify the one where you talk about the append function? It looks like a typo
Hi,
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.
Mark
UPDATE! the second section of the post was gibberish – probably due to a formatting error – now I corrected it in a reproducible way.
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))) } }
Oh, and a short comment to your “do.call” 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 <- do.call("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.