Skip to content

Instantly share code, notes, and snippets.

@BroVic
Last active November 8, 2021 11:00
Show Gist options
  • Select an option

  • Save BroVic/eea59485c22ad62bc0deda79060578ca to your computer and use it in GitHub Desktop.

Select an option

Save BroVic/eea59485c22ad62bc0deda79060578ca to your computer and use it in GitHub Desktop.
A modification of the builtin mtcars dataset that converts a couple of variables to factors
stopifnot(getRversion() >= 4.1)
dat <- mtcars
levs <-
structure(
.Data = list(c("V-shaped", "straight"), c("automatic", "manual")),
names = fcols <- c('vs', 'am')
)
for (i in fcols)
dat[[i]] <- (dat[[i]] == 0) |>
ifelse(levs[[i]][1], levs[[i]][2]) |>
as.factor()
rm(levs, fcols, i)
print(head(dat))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment