Articles by Econometrics and Free Software

Lesser known purrr tricks

March 24, 2017 | Econometrics and Free Software

purrr is a package that extends R’s functional programming capabilities. It brings a lot of new stuff to the table and in this post I show you some of the most useful (at least to me) functions included in purrr. Getting rid of loops with map()
library(purrr)

numbers <- list(11, 12, 13, 14)

map_dbl(numbers, sqrt)
## [1] 3.316625 3.464102 3.605551 3.741657
You ... [Read more...]

Lesser known dplyr tricks

March 9, 2017 | Econometrics and Free Software

In this blog post I share some lesser-known (at least I believe they are) tricks that use mainly functions from dplyr. Removing unneeded columns Did you know that you can use - in front of a column name to remove it from a data frame?
mtcars %>% 
    select(-disp) %>% 
    head()
##                    mpg cyl  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6 105 2.76 3.460 20.22  1  0    3    1
Re-ordering columns Still ... [Read more...]

Lesser known dplyr tricks

March 8, 2017 | Econometrics and Free Software

In this blog post I share some lesser-known (at least I believe they are) tricks that use mainly functions from dplyr. Removing unneeded columns Did you know that you can use - in front of a column name to remove it from a data frame?
mtcars %>% 
    select(-disp) %>% 
    head()
##                    mpg cyl  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6 105 2.76 3.460 20.22  1  0    3    1
Re-ordering columns Still ... [Read more...]

How to use jailbreakr

February 17, 2017 | Econometrics and Free Software

What is jailbreakr The jailbreakr package is probably one of the most interesting packages I came across recently. This package makes it possible to extract messy data from spreadsheets. What is meant by messy? I am sure you already had to deal with spreadsheets that contained little tables inside a ... [Read more...]

How to use jailbreakr

February 17, 2017 | Econometrics and Free Software

What is jailbreakr The jailbreakr package is probably one of the most interesting packages I came across recently. This package makes it possible to extract messy data from spreadsheets. What is meant by messy? I am sure you already had to deal with spreadsheets that contained little tables inside a ... [Read more...]

My free book has a cover!

December 23, 2016 | Econometrics and Free Software

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...]

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 8 9 10

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)