Articles by Econometrics and Free Software

Read a lot of datasets at once with R

July 25, 2016 | Econometrics and Free Software

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 | Econometrics and Free Software

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

March 30, 2016 | Econometrics and Free Software

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 | Econometrics and Free Software

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...]
1 10 11 12

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)