tonum | R Documentation |
Convert ill-encoded factor to numeric
tonum(data, pattern = NULL, replacement = NULL, comma2period = TRUE)
data |
a vector (character, factor, oredered) or a data.frame |
pattern |
character string containing a regular expression (or character string for fixed = TRUE) to be matched in the given character vector. |
replacement |
a replacement for matched pattern |
comma2period |
change "," to "." |
Apply as.numeric(as.character())
JuG
devtools::install_github("vpnagraj/rrefine")
library(rrefine)
lateformeeting$sleephours
tonum(lateformeeting$sleephours)
tonum(lateformeeting$sleephours,pattern = c("six", "7 and a half"), replacement = c("6","7.5"))
dosage <- c( 1.2, 1,5, 5.6, "<1", 12, "13,2", 1.1,12, "<1",3, ">50") #in pg
tonum(dosage, pattern = c("<1",">"), replacement = c("0",""))
semiquant <- ordered(cut(tonum(dosage, pattern = c("<1",">"), replacement = c("0","")),c(-0.1,1,5,100),labels = c("<1 pg","1-5 pg",">5 pg")))
data.frame(dosage,semiquant)
dosageData <- data.frame(dosage1 = dosage, dosage2= sample(dosage))
tonum(dosageData) #no replacement
tonum(dosageData, pattern = c("<1",">"), replacement = c("0","")) #with replacemen
@return numeric() vector or matrixaze
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.