The wildcard package is a templating mechanism for data frames. Wildcards are placeholders for text, and you can evaluate them to generate new data frames from templates. The functionality is straightforward.

wildcard()

library(wildcard)
myths <- data.frame(
  myth = c("Bigfoot", "UFO", "Loch Ness Monster"), 
  claim = c("various", "day", "day"), 
  note = c("various", "pictures", "reported day"))
myths
##                myth   claim         note
## 1           Bigfoot various      various
## 2               UFO     day     pictures
## 3 Loch Ness Monster     day reported day
wildcard(myths, wildcard = "day", values = c("today", "yesterday"))
##                myth     claim               note
## 1           Bigfoot   various            various
## 2               UFO     today           pictures
## 3               UFO yesterday           pictures
## 4 Loch Ness Monster     today     reported today
## 5 Loch Ness Monster yesterday reported yesterday
wildcard(myths, wildcard = "day", values = c("today", "yesterday"),
  expand = FALSE)
##                myth     claim           note
## 1           Bigfoot   various        various
## 2               UFO     today       pictures
## 3 Loch Ness Monster yesterday reported today
wildcard(myths, wildcard = "day", values = c("today", "yesterday"),
  include = "claim")
##                myth     claim         note
## 1           Bigfoot   various      various
## 2               UFO     today     pictures
## 3               UFO yesterday     pictures
## 4 Loch Ness Monster     today reported day
## 5 Loch Ness Monster yesterday reported day
wildcard(myths, wildcard = "day", values = c("today", "yesterday"),
  exclude = c("claim", "note"))
##                myth   claim         note
## 1           Bigfoot various      various
## 2               UFO     day     pictures
## 3 Loch Ness Monster     day reported day
locations <- data.frame(
  myth = c("Bigfoot", "UFO", "Loch Ness Monster"),
  origin = "where")
rules <- list(
  where = c("North America", "various", "Scotland"),
  UFO = c("spaceship", "saucer"))
wildcard(locations, rules = rules, expand = c(FALSE, TRUE))
##                myth        origin
## 1           Bigfoot North America
## 2         spaceship       various
## 3            saucer       various
## 4 Loch Ness Monster      Scotland
numbers <- data.frame(x = 4, y = 3, z = 4444, w = 4.434)
wildcard(numbers, wildcard = 4, values = 7)
##   x y    z     w
## 1 7 3 7777 7.737

expandrows()

df <- data.frame(
  ID = c("24601", "Javert", "Fantine"), 
  fate = c("fulfillment", "confusion", "misfortune"))
expandrows(df, n = 2, type = "each")
##        ID        fate
## 1   24601 fulfillment
## 2   24601 fulfillment
## 3  Javert   confusion
## 4  Javert   confusion
## 5 Fantine  misfortune
## 6 Fantine  misfortune
expandrows(df, n = 2, type = "times")
##        ID        fate
## 1   24601 fulfillment
## 2  Javert   confusion
## 3 Fantine  misfortune
## 4   24601 fulfillment
## 5  Javert   confusion
## 6 Fantine  misfortune

nofactors()

class(iris$Species)
## [1] "factor"
str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
out <- nofactors(iris)
class(out$Species)
## [1] "character"
str(out)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : chr  "setosa" "setosa" "setosa" "setosa" ...

Troubleshooting

You can submit questions, bug reports, and feature requests to the issue tracker. Please take care to search for duplicates first, even among the closed issues.

A cautionary note

Be sure that wildcards and are not also replacement values.

df <- data.frame(x = "a", y = "b")
rules <- list(a = letters[1:3], b = LETTERS[1:3])
wildcard(df, rules = rules)
##   x y
## 1 a A
## 2 a B
## 3 a C
## 4 A A
## 5 B B
## 6 C C
## 7 c A
## 8 c B
##   c C
## Warning message:
## In check_rules(rules) :
##   In `rules`, some wildcards are also replacement values.
## The returned data frame may be different than you expect,
## and it may depend on the order of the wildcards in `rules`.