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

##### February 3, 2019

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")

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 flexiblyrbind 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 D H B E
## 1 a d h b e
## 2 a d h b e
## 3 a d h b e
## 4 a d h b e
## 5 a d h b e
##
## [[2]]
##   A H I C G
## 1 a h i c g
## 2 a h i c g
## 3 a h i c g
## 4 a h i c g
## 5 a h i c g
##
## [[3]]
##   J I E B F
## 1 j i e b f
## 2 j i e b f
## 3 j i e b f
## 4 j i e b f
## 5 j i e b f
##
## [[4]]
##   F I B J A
## 1 f i b j a
## 2 f i b j a
## 3 f i b j a
## 4 f i b j a
## 5 f i b j a
##
## [[5]]
##   D E G I J
## 1 d e g i j
## 2 d e g i j
## 3 d e g i j
## 4 d e g i j
## 5 d e g i j

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 D B E  C
## 1 a d b e NA
## 2 a d b e NA
## 3 a d b e NA
## 4 a d b e NA
## 5 a d b e NA
##
## [[2]]
##   A C  B  D  E
## 1 a c NA NA NA
## 2 a c NA NA NA
## 3 a c NA NA NA
## 4 a c NA NA NA
## 5 a c NA NA NA
##
## [[3]]
##   E B  A  C  D
## 1 e b NA NA NA
## 2 e b NA NA NA
## 3 e b NA NA NA
## 4 e b NA NA NA
## 5 e b NA NA NA
##
## [[4]]
##   B A  C  D  E
## 1 b a NA NA NA
## 2 b a NA NA NA
## 3 b a NA NA NA
## 4 b a NA NA NA
## 5 b a NA NA NA
##
## [[5]]
##   D E  A  B  C
## 1 d e NA NA NA
## 2 d e NA NA NA
## 3 d e NA NA NA
## 4 d e NA NA NA
## 5 d e NA NA NA

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

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