This function handles the creation of data frames based on intuitive parameters. It was originally designed to make row binding easier when columns differs among data frame by creating data frames with the same columns.

dfTemplate(cols, nrows = 1, col_classes = NULL, fill = NA)

dfTemplateMatch(x, y, yonly = FALSE, order = FALSE, ...)

Arguments

cols

either a number of column or a vector of character used as columns names of the data frame to be returned.

nrows

row number.

col_classes

vector of column classes for the desired data frame. By default, the class is determined by fill.

fill

character or number used to fill out the columns. Default is NA.

x

a data frame.

y

a data frame or a vector of strings use to specifies column names to be included in the data frame.

yonly

a logical. Should only y (or the names(y)) be used for the data frame to be returned? Default is set to FALSE meaning that both the names of x and the names of y are used.

order

a logical. Should column of the output data frame be ordered according to the template. Not that if there are more columns in x that are not in y, then if order = TRUE column of y will be added first (on the left of the output data frame).

...

further arguments to be passed to dfTemplate().

Value

Returns a data frame with the desired characteristics.

Functions

  • dfTemplateMatch: Returns a data frame that includes all columns specifies in y.

References

https://insileco.github.io/2019/02/03/creating-empty-data-frames-with-dftemplate-and-dftemplatematch/

Examples

dfTemplate(5, 2)
#> Var1 Var2 Var3 Var4 Var5 #> 1 NA NA NA NA NA #> 2 NA NA NA NA NA
dfTemplate(5, 2, col_classes = "character")
#> Var1 Var2 Var3 Var4 Var5 #> 1 <NA> <NA> <NA> <NA> <NA> #> 2 <NA> <NA> <NA> <NA> <NA>
dfA <- data.frame(col1 = c(1, 2), col2 = LETTERS[1:2]) dfB <- data.frame(col4 = c(1, 2), col2 = LETTERS[1:2]) dfTemplateMatch(dfA, c("col4", "col2"))
#> col1 col2 col4 #> 1 1 A NA #> 2 2 B NA
dfTemplateMatch(dfA, c("col4", "col2"), yonly = TRUE)
#> col2 col4 #> 1 A NA #> 2 B NA
dfTemplateMatch(dfA, dfB, yonly = TRUE, order = TRUE)
#> col4 col2 #> 1 NA A #> 2 NA B