miscellaneous {ems} | R Documentation |
Collections of functions for data editing usually used as lower levels for other functions.
f.num
is a wrapper to format numeric variables that are stored as character or factor, simultaneously it will try to detect comma spearated and replace it by dots before formating the variable as numeric. Any non-numeric encoding will be coerced to NA.
f.date
is a wrapper either to as.Date
or strptime
to format character or factor variables into dates. In Epimed Solutions database there are a few pre-specified formats that f.date
will try to detect and return a formated date. f.date
will try to dected if more than half of the elements in a vector have a pre-specified format. If so, the remaining will be coerced to NA if they have different format from the detected. See example.
remove.na
identifies all the empties spaces, i.e. the " " cells, of character or factor variables in a data.frame and returns the same data.frame with these empty cells replaced, by default, by NAs. It does not matter the length of the empty spaces. Also, remove.na
trims the leading and trailing empty spaces from all character and factor variables. It does not format the numeric variables. It may also returns at the console a few information about the " " fields.
tab2tex
removes the empty rows, and also tunrs the rownames of a table epiDisplay::tableStack into the first column, to make it easier to paste the table into a rtf or latex document without empty rows or rownames conflicts.
trunc_num
truncates a numeric vector by replacing the values below the min value or above the max values by the min and max values respectively or optionall to NA. See example.
dummy.columns
takes a data.frame
with one column with concatatenated levels of a factor (or character) variable and return a data.frame
with additional columns with zeros and ones (dummy values), which names are the factor levels of the original column. See example below. rm.dummy.columns
is an internal function of dummy.columns
that deletes the new dummy columns which have less then a specified minimum events.
f.num(num.var) f.date(date) remove.na(data, replace = NA, console.output = TRUE) tab2tex(x, nc = ncol(x)) trunc_num(x, min, max, toNA = FALSE) dummy.columns(data, original.column, factors, scan.oc = FALSE, sep = ",", colnames.add = "Dummy.", min.events = NULL, rm.oc = FALSE, warn = FALSE, return.factor = TRUE) rm.dummy.columns(data, colnames, event = "1", min.events = 50, warn = FALSE)
num.var |
A character, or factor variable to be formated as numeric. |
date |
A character or factor variable to be formated as date. |
data |
A |
replace |
By default, NA. But could be any vector of length 1. |
console.output |
Logical. Print at the console a few information about the "" fields? |
x, nc |
For |
min, max |
For |
toNA |
For |
original.column |
A character vector representing the name of the column the be transformed in dummy variables. |
factors |
A character vector to make new dummy columns and to match values in |
scan.oc |
Default = FALSE, if TRUE, |
sep |
A character of legth one that systematically split the factors in the original columns. It wil be passed to the |
colnames.add |
The default is '= "Dummy_"'. This is a character vector of length one to stick in the |
min.events |
Either |
rm.oc |
Default is |
warn |
Default is |
return.factor |
Default is |
colnames |
For |
event |
A character string to be detected as a event. In |
Lunna Borges & Pedro Brasil
# Formating character or factor variable that should be numeric variables f.num(c("2,4000","10,0000","5.0400")) # Simulating a dataset y <- data.frame(v1 = sample(c(" F","M "," "), 10, replace = TRUE), v2 = sample(c(1:3," "), 10, replace = TRUE), v3 = sample(c("Alive","Dead",""), 10, replace = TRUE)) y # Replacing the "" cells by NA y <- remove.na(y) y rm(y) # Formating dates x <- f.date(c("28/02/2013","16/07/1998","31/03/2010")) x class(x) # The first element (i.e., the different one) is coerced to NA x <- f.date(c("2013-02-28 12:40","16/07/1998","31/03/2010")) x class(x) # The last element (i.e. the different one) is coerced to NA x <- f.date(c("2013-02-28 12:40","1998-07-16 18:50","31/03/2010")) x class(x) # Truncating numeric vectors trunc_num(1:12, min = 3, max = 10) # Truncating numeric vectors but returning NAs instead trunc_num(1:12, min = 3, max = 10, toNA = TRUE) # Simulating a dataset for dummy.columns example y <- data.frame(v1 = 1:20, v2 = sapply(1:20, function(i) toString(sample(c("Code1","Code2","Code3","Code4"), size = sample(2:4, 1), replace = FALSE)))) y # For a few of the codes in the original column y <- dummy.columns(y, original.column = "v2", factor = c("Code2","Code3")) y # For all codes in the original column y <- dummy.columns(y[, 1:2], original.column = "v2", scan.oc = TRUE) y rm(y)