h_reformat_tab.Rd
Reformat a Variable in a Specific Column and Table
h_reformat_tab(db, tab, col, dic_map)
(dm
) object input.
(string
) the name of a table.
(string
) the name of a variable in a table.
(named vector
) a dictionary with the mapping values, with the format c(new = old)
.
a dm
object with re coded variables as factor.
If tab
is not a valid table name of the db
object, the original object is returned. Similarly, if col
is
not a valid column of the selected tab
in the object, the original object is returned. This behavior is desirable
when a variable that exists in most but not all tables has to be re coded.
Both empty string and NAs
can be re coded if needed.
the label
attribute of the column is preserved.
library(dm)
df1 <- data.frame(
"char" = c("", "b", NA, "a", "k", "x"),
"fact" = factor(c("f1", "f2", NA, NA, "f1", "f1")),
"logi" = c(NA, FALSE, TRUE, NA, FALSE, NA)
)
df2 <- data.frame(
"char" = c("a", "b", NA, "a", "k", "x"),
"fact" = factor(c("f1", "f2", NA, NA, "f1", "f1")),
"num" = 1:6
)
db <- dm(df1, df2)
dic_map <- setNames(c("A", "B", "Missing", "Empty"), c("a", "b", NA, ""))
res <- h_reformat_tab(db, "df1", "char", dic_map)