Creating empty data frames with dfTemplate() and dfTemplateMatch()

February 3, 2019

  R data frame data frames generation row binding

  inSilecoMisc

Kevin Cazelles  

Azenor Bideault   Marie-Hélène Brice  


Creating a data frame is fairly simple but when you need to create a large empty data frame with columns that have different classes it takes several command lines. A few days ago, I decided to write a function to simplify this operation and I came to realize that such a function would actually be very useful to ease the row binding of data frames whose column names partially match. How so? This post is meant to answer this question!

inSilecoMisc

First of all, the functions I am using in this post are available in inSilecoMisc which is an R package where we gathered the miscellaneous functions we wrote and deem worth sharing on GitHub. So the first step to reproduce the examples below is to install inSilecoMisc which is straightforward with the devtools :

library(devtools)
install_github("inSileco/inSilecoMisc")

Then, load it:

library(inSilecoMisc)

In this post, I’ll exemplify how to use dfTemplate() and dfTemplateMatch() but if you are interested in other functions in the packages, check out the tour vignette.

Generating empty data frames efficiently

Let’s start with dfTemplate() that creates a data frame with a specific number of columns.

df1 <- dfTemplate(cols = 2)
df1
##   Var1 Var2
## 1   NA   NA
class(df1)
## [1] "data.frame"

By default, the data frame created has only 1 row and the columns are filled out with NA. This can readily be changed using arguments nrows and fill.

df2 <- dfTemplate(2, nrows = 4, fill = 0)
df2
##   Var1 Var2
## 1    0    0
## 2    0    0
## 3    0    0
## 4    0    0
df3 <- dfTemplate(cols = 2, nrows = 3, fill = "")
df3
##   Var1 Var2
## 1          
## 2          
## 3

Columns classes are determined by fill:

class(df1[,1])
class(df2[,1])
class(df3[,1])
## [1] "logical"
## [1] "numeric"
## [1] "factor"

And col_classes is used to changed these classes:

df4 <- dfTemplate(cols = 2, col_classes = "character")
class(df4[, 1])
class(df4[, 2])
## [1] "factor"
## [1] "factor"

Arguments fill and col_classes can be vectors that specify content and class for every columns:

df5 <- dfTemplate(2, 5, col_classes = c("character", "numeric"), fill = c("", 5))
df5
class(df5[, 1])
class(df5[, 2])
##   Var1 Var2
## 1         5
## 2         5
## 3         5
## 4         5
## 5         5
## [1] "factor"
## [1] "numeric"

Another useful feature of dfTemplate() is that column names of the data frame to be created can be passed as first argument (cols) :

df5 <- dfTemplate(c("category", "value"))

So, now you are able to create custom data frames with a set of column names!

nms <- LETTERS[1:10]
df6 <- dfTemplate(nms, 10, fill = tolower(nms))
df6
##    A B C D E F G H I J
## 1  a b c d e f g h i j
## 2  a b c d e f g h i j
## 3  a b c d e f g h i j
## 4  a b c d e f g h i j
## 5  a b c d e f g h i j
## 6  a b c d e f g h i j
## 7  a b c d e f g h i j
## 8  a b c d e f g h i j
## 9  a b c d e f g h i j
## 10 a b c d e f g h i j

How to flexibly rbind a list of data frames

Sometimes we need to rbind data frames that do not have all the columns the final data frame must contain. In such case, we first need to append the missing columns because otherwise the default rbind function won’t work. Another solution is to use a package that has a function that do so. For instance, rbind.fill() from the plyr package allows to perform such flexible rbind. Also, the package data.table includes a rbind() method for data.table objects that handles such situation (see this answer on ). In this last section, I would like to show how to rbind data frames flexibly with dfTemplateMatch() that is written in base R.

Let me first introduces dfTemplateMatch(). This function takes a data frame as the first argument (x) and the second argument (y) could be another data frame or a vector of character strings. Based on x and y, dfTemplateMatch() creates a data frame that has the same number of rows as x and add columns for all names found in y that are not found in x. Before calling dfTemplateMatch() I create two data frames :

df7 <- df6[1:5, 1:4]
df7
##   A B C D
## 1 a b c d
## 2 a b c d
## 3 a b c d
## 4 a b c d
## 5 a b c d
df8 <- df6[4:6]
df8
##    D E F
## 1  d e f
## 2  d e f
## 3  d e f
## 4  d e f
## 5  d e f
## 6  d e f
## 7  d e f
## 8  d e f
## 9  d e f
## 10 d e f

Now I use dfTemplateMatch() to create a third data frame based on two other:

dfTemplateMatch(df7, df8)
##   A B C D  E  F
## 1 a b c d NA NA
## 2 a b c d NA NA
## 3 a b c d NA NA
## 4 a b c d NA NA
## 5 a b c d NA NA

As expected, the output has 5 rows as df6 and columns that are not in df6 but in df7 has been appended to df6. It is possible to use arguments fill and col_classes to custom the columns added.

dfTemplateMatch(df7, df8, fill = 1, col_classes = "numeric")
##   A B C D E F
## 1 a b c d 1 1
## 2 a b c d 1 1
## 3 a b c d 1 1
## 4 a b c d 1 1
## 5 a b c d 1 1

And there is an argument yonly that allows the user to keep only names of y (when yonly = TRUE).

dfTemplateMatch(df7, df8, yonly = TRUE, fill = 1, col_classes = "numeric")
##   D E F
## 1 d 1 1
## 2 d 1 1
## 3 d 1 1
## 4 d 1 1
## 5 d 1 1

Now let me show you how to rbind() a specific subset of columns of a list of data frame that may or may not have these columns. Let me start by creating a list of data frames.

lsdf <- apply(
  replicate(5, sample(nms, 5)),
  2,
  function(x) dfTemplate(x, nrows = 5, fill = tolower(x))
)
lsdf
## [[1]]
##   A B D F H
## 1 a b d f h
## 2 a b d f h
## 3 a b d f h
## 4 a b d f h
## 5 a b d f h
## 
## [[2]]
##   D A E H B
## 1 d a e h b
## 2 d a e h b
## 3 d a e h b
## 4 d a e h b
## 5 d a e h b
## 
## [[3]]
##   G A E F H
## 1 g a e f h
## 2 g a e f h
## 3 g a e f h
## 4 g a e f h
## 5 g a e f h
## 
## [[4]]
##   D I B J H
## 1 d i b j h
## 2 d i b j h
## 3 d i b j h
## 4 d i b j h
## 5 d i b j h
## 
## [[5]]
##   J D F C H
## 1 j d f c h
## 2 j d f c h
## 3 j d f c h
## 4 j d f c h
## 5 j d f c h

So the goal here is to create a data frame that contains only the five first columns, i.e. A, B, C, D, E, the remaining columns must be discarded and when one selected column is missing, it must be added (filled out with NA). To do so, I simply need to call dfTemplateMatch():

lsdf2 <- lapply(lsdf, dfTemplateMatch, LETTERS[1:5], yonly = TRUE)
lsdf2
## [[1]]
##   A B D  C  E
## 1 a b d NA NA
## 2 a b d NA NA
## 3 a b d NA NA
## 4 a b d NA NA
## 5 a b d NA NA
## 
## [[2]]
##   D A E B  C
## 1 d a e b NA
## 2 d a e b NA
## 3 d a e b NA
## 4 d a e b NA
## 5 d a e b NA
## 
## [[3]]
##   A E  B  C  D
## 1 a e NA NA NA
## 2 a e NA NA NA
## 3 a e NA NA NA
## 4 a e NA NA NA
## 5 a e NA NA NA
## 
## [[4]]
##   D B  A  C  E
## 1 d b NA NA NA
## 2 d b NA NA NA
## 3 d b NA NA NA
## 4 d b NA NA NA
## 5 d b NA NA NA
## 
## [[5]]
##   D C  A  B  E
## 1 d c NA NA NA
## 2 d c NA NA NA
## 3 d c NA NA NA
## 4 d c NA NA NA
## 5 d c NA NA NA

And now I can seamlessly rbind() the list lsdf2!

do.call(rbind, lsdf2)
##       A    B    D    C    E
## 1     a    b    d <NA> <NA>
## 2     a    b    d <NA> <NA>
## 3     a    b    d <NA> <NA>
## 4     a    b    d <NA> <NA>
## 5     a    b    d <NA> <NA>
## 6     a    b    d <NA>    e
## 7     a    b    d <NA>    e
## 8     a    b    d <NA>    e
## 9     a    b    d <NA>    e
## 10    a    b    d <NA>    e
## 11    a <NA> <NA> <NA>    e
## 12    a <NA> <NA> <NA>    e
## 13    a <NA> <NA> <NA>    e
## 14    a <NA> <NA> <NA>    e
## 15    a <NA> <NA> <NA>    e
## 16 <NA>    b    d <NA> <NA>
## 17 <NA>    b    d <NA> <NA>
## 18 <NA>    b    d <NA> <NA>
## 19 <NA>    b    d <NA> <NA>
## 20 <NA>    b    d <NA> <NA>
## 21 <NA> <NA>    d    c <NA>
## 22 <NA> <NA>    d    c <NA>
## 23 <NA> <NA>    d    c <NA>
## 24 <NA> <NA>    d    c <NA>
## 25 <NA> <NA>    d    c <NA>

Voilà! This is what I call a flexible rbind! I hope you’ll find this helpful! 💥