Articles by Bruno Rodrigues

My free book has a cover!

January 6, 2017 | Bruno Rodrigues

I’m currently writing a book as a hobby. It’s titled Functional programming and unit testing for data munging with R and you can get it for free here. You can also read it online for free on my webpage What’s the book about? Here’s the teaser... [Read more...]

Merge a list of datasets together

July 29, 2016 | Bruno Rodrigues

Last week I showed how to read a lot of datasets at once with R, and this week I’ll continue from there and show a very simple function that uses this list of read datasets and merges them all together. First we’ll use read_list() to read all ... [Read more...]

Read a lot of datasets at once with R

July 25, 2016 | Bruno Rodrigues

I often have to read a lot of datasets at once using R. So I’ve wrote the following function to solve this issue:
read_list <- function(list_of_datasets, read_func){

        read_and_assign <- function(dataset, read_func){
                dataset_name <- as.name(dataset)
                dataset_name <- read_func(dataset)
        }

        # invisible is used to suppress the unneeded output
        output <- invisible(
                sapply(list_of_datasets,
                           read_and_assign, read_func = read_func, simplify = FALSE, USE.NAMES = TRUE))

        # Remove the extension at the end of the data set names
        names_of_datasets <- c(unlist(strsplit(list_of_datasets, "[.]"))[c(T, F)])
        names(output) <- names_of_datasets
        return(output)
}
You need to supply a list of datasets as well as the function to read the datasets to read_list. So for example to read ... [Read more...]

Data frame columns as arguments to dplyr functions

July 17, 2016 | Bruno Rodrigues

Suppose that you would like to create a function which does a series of computations on a data frame. You would like to pass a column as this function’s argument. Something like:
data(cars)


convertToKmh <- function(dataset, col_name){
  dataset$col_name <- dataset$speed * 1.609344
  return(dataset)
}
This example is obviously not very interesting (you don’t need a function for this), but ... [Read more...]

Careful with tryCatch

June 20, 2016 | Bruno Rodrigues

tryCatch is one of the functions that allows the users to handle errors in a simple way. With it, you can do things like: if(error), then(do this). Take the following example:
sqrt("a")
Error in sqrt("a") : non-numeric argument to mathematical function
Now maybe you’d want something to happen when such an error happens. You can achieve ... [Read more...]

Unit testing with R

March 30, 2016 | Bruno Rodrigues

I've been introduced to unit testing while working with colleagues on quite a big project for which we use Python. At first I was a bit skeptical about the need of writing unit tests, but now I must admit that I am seduced by the idea and by the huge ... [Read more...]

Export R output to a file

February 21, 2015 | Bruno Rodrigues

Sometimes it is useful to export the output of a long-running R command. For example, you might want to run a time consuming regression just before leaving work on Friday night, but would like to get the output saved inside your Dropbox folder to take a look at the results ... [Read more...]

Introduction to programming econometrics with R

January 11, 2015 | Bruno Rodrigues

This semester, I’ll be teaching an introduction to applied econometrics with R, so I’ve decided to write a very small book called “Introduction to programming Econometrics with R”. This is primarily intended for bachelor students and the focus is not much on econometric theory, but more on how ... [Read more...]

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)