I'm trying to write an R function to convert fractions and mixed numbers to decimals. e.g.
mixedToFloat <- function(x){
x <- sub(' ', '+', x, fixed=TRUE)
return(unlist(lapply(x, function(x) eval(parse(text=x)))))
}
> mixedToFloat(c('1 1/2', '2 3/4', '2/3', '11 1/4', '1'))
[1] 1.5000000 2.7500000 0.6666667 11.2500000 1.0000000
This works for most of the cases I can think of, but feels a little bit hackish. Is there a more standard way to do this?