[R] covert entire dataset to numeric while persuing percentage values

Methekar, Pushpa (GE Transportation, Non-GE) pushpa.methekar at ge.com
Thu Feb 26 10:06:03 CET 2015


Hi ,
I am little confused  about how to covert entire dataset to numeric .
As I read data like..
Xelements =read.csv(file. Choose(),header = T, stringsAsFactors=FALSE)
str(xelements )

> str(xelements)
'data.frame':  731 obs. of  4 variables:
$ Engine.Speed     : chr  "rpm" "ES" "rpm" "1049" ...
$ X..NG.by.Energy  : chr  "" "% NG by Energy" "%" "0%" ...
$ Int.Mfld.Temp    : chr  "" "Int Mfld Temp" "°C" "49" ...
$ Cmd.Advance.Angle: chr  "" "Cmd Advance Angle" "°BTDC" "13.8" ...

I have second column as in 0%, 10%,.... In percentage value ,
Whenever I am going to covert whole dataset its showing NA introduced .second column going to become NA.
Converting separately I would be successful .


> xelements$Engine.Speed <- as.numeric(xelements$Engine.Speed)

Warning message:

NAs introduced by coercion

> xelements$X..NG.by.Energy<- as.numeric(sub("%","",xelements$X..NG.by.Energy))/100

Warning message:

NAs introduced by coercion

> xelements$Int.Mfld.Temp<- as.numeric(xelements$Int.Mfld.Temp)

Warning message:

NAs introduced by coercion

> xelements$Cmd.Advance.Angle<- as.numeric(xelements$Cmd.Advance.Angle)

Warning message:

NAs introduced by coercion

But I want to covert whole dataset at a time. I want to write function which will help me to solve this problem  .


xelements <- data.frame(sapply(xelements, function(x) as.numeric(as.character(x))))
sapply(xelements, class)

but it won't be able to covert percentage value  like 10%, 20%....
please do help me if you know the way. Thank you

	[[alternative HTML version deleted]]



More information about the R-help mailing list