[R] Converting chr to num

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sun Aug 19 07:06:23 CEST 2018


Hello,

It also works with class "factor":

df <- data.frame(variable = c("12.6%", "30.9%", "61.4%"))
class(df$variable)
#[1] "factor"

as.numeric(gsub(pattern = "%", "", df$variable))
#[1] 12.6 30.9 61.4


This is because sub() and gsub() return a character vector and the 
instruction becomes an equivalent of what the help page ?factor 
documents in section Warning:

To transform a factor f to approximately its original numeric values, 
as.numeric(levels(f))[f] is recommended and slightly more efficient than 
as.numeric(as.character(f)).


Also, I would still prefer

as.numeric(sub(pattern = "%$","",df$variable))
#[1] 12.6 30.9 61.4

The pattern is more strict and there is no need to search&replace 
multiple occurrences of '%'.



Hope this helps,

Rui Barradas

On 18/08/2018 23:08, GALIB KHAN wrote:
> So running the code in my head....as long as that column's data type is a
> vector of characters then it should work.
> 
> 
> Did you try it out?
> 
> On Sat, Aug 18, 2018, 5:02 PM Jeff Reichman <reichmanj using sbcglobal.net> wrote:
> 
>> Given it’s a variable would I just change the 12.6 in
>> as.numeric(gsub(pattern = "%","","12.6%"))
>>
>> To the variable name say … as.numeric(gsub(pattern = "%","",df$variable))
>>
>>
>>
>>
>>
>> *From:* GALIB KHAN <ghk18 using scarletmail.rutgers.edu>
>> *Sent:* Saturday, August 18, 2018 4:23 PM
>> *To:* reichmanj using sbcglobal.net
>> *Cc:* r-help using r-project.org
>> *Subject:* Re: [R] Converting chr to num
>>
>>
>>
>> Hey there,
>>
>>
>>
>> as.numeric(gsub(pattern = "%","","12.6%"))
>>
>>
>>
>> On Sat, Aug 18, 2018 at 4:20 PM, Jeff Reichman <reichmanj using sbcglobal.net>
>> wrote:
>>
>> R-Help Forum
>>
>>
>>
>> How do I convert a chr variable that contains percentages to an integer
>>
>>
>>
>> Example 12.6% (chr) to 12.6 (int)
>>
>>
>>
>> Jeff
>>
>>
>>          [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using 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.
>>
>>
>>
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using 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.
> 

---
This email has been checked for viruses by AVG.
https://www.avg.com




More information about the R-help mailing list