I have a data table whose one column consists of missing cells and character strings like 7 1/4 INCHES
, 1/4 INC
, 9/16 INCH
, 1 3/4 INCHES
, 1 13/16 INCHES
, 20 INCHES
. I want to get rid of whitespaces and INC/INCH/INCHES (string split) and evaluate the rest of the string by converting them to numeric like 7+1/4=7.25
.
library(data.table)
data<-data.table(variable = c("", "", "7 1/4 INCHES", "1/4 INC", "9/16 INCH", "1 3/4 INCHES", "", "1 13/16 INCHES", "20 INCHES", "", ""))
#Assigning 0s to empty cells
data$variable[data$variable == "" ] = 0
#Getting rid of INCH, INCHES and INCH
data$variable<-gsub("[[:space:]][A-z]*$", "", data$variable)
#Adding "+" instead of whitespace (for summation), like 7+1/4 instead of 7 1/4
data$variable<-gsub( " ", "+", data$variable)
data$variable<-eval(parse(text=data$variable))
However, I cannot make eval
function to work. Could you please help me about it?
Secondly, this particular code does not seem to be a very efficient way to do. I have a very big dataset, and it has 4 columns with a lot of observations, like the small example above. How can I fasten things up a bit?
EDIT:
data$variable<-sapply(data$variable, function(x) eval(parse(text=x)))
I make it work using the line above. However, it is still not an efficient way.