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

jim holtman jholtman at gmail.com
Thu Feb 26 15:34:49 CET 2015


It would help a lot if you posted a subset of your data using 'dput'
so that we know what it actually looks like.  You have character data
mixed with numerics, so you will be NAs in some cases.

Conversion of percent to numeric is accomplished with something like this:

> x <- c('12%', '6%', '3.75%')
> # convert to a number
> as.numeric(gsub("%", "", x)) / 100
[1] 0.1200 0.0600 0.0375
>


Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Thu, Feb 26, 2015 at 4:06 AM, Methekar, Pushpa (GE Transportation,
Non-GE) <pushpa.methekar at ge.com> wrote:
> 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]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list