Converting chr to num

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Converting chr to num

reichmaj
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting chr to num

GALIB KHAN
Hey there,

as.numeric(gsub(pattern = "%","","12.6%"))

On Sat, Aug 18, 2018 at 4:20 PM, Jeff Reichman <[hidden email]>
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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting chr to num

Rui Barradas
In reply to this post by reichmaj
Hello,

You have to get rid of the percent sign first. This can be done with ?sub

x <- "12.6%"
y <- sub("%$", "", x)
z <- as.numeric(y)

1) The dollar sign means "end of string". See ?regexpr.
2) You can all of that in one code line, no need to create y.

z <- as.numeric(sub("%$", "", x))


Hope this helps,

Rui Barradas

On 18/08/2018 22:20, Jeff Reichman 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]]
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting chr to num

reichmaj
In reply to this post by GALIB KHAN
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 <[hidden email]>
Sent: Saturday, August 18, 2018 4:23 PM
To: [hidden email]
Cc: [hidden email]
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 <[hidden email] <mailto:[hidden email]> > 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]]

______________________________________________
[hidden email] <mailto:[hidden email]>  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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting chr to num

GALIB KHAN
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 <[hidden email]> 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 <[hidden email]>
> *Sent:* Saturday, August 18, 2018 4:23 PM
> *To:* [hidden email]
> *Cc:* [hidden email]
> *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 <[hidden email]>
> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting chr to num

Bert Gunter-2
In reply to this post by reichmaj
ummmm.... 12.6 is not an integer.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Aug 18, 2018 at 2:20 PM Jeff Reichman <[hidden email]>
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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting chr to num

Rui Barradas
In reply to this post by GALIB KHAN
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 <[hidden email]> 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 <[hidden email]>
>> *Sent:* Saturday, August 18, 2018 4:23 PM
>> *To:* [hidden email]
>> *Cc:* [hidden email]
>> *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 <[hidden email]>
>> 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]]
>>
>> ______________________________________________
>> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting chr to num

Daniel Nordlund-3
See comment inline below:

On 8/18/2018 10:06 PM, Rui Barradas wrote:

> 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 '%'.

The pattern is more strict, and that could cause the conversion to fail
if the process that created the strings resulted in trailing spaces.
Without the '$' the conversion succeeds.

df <- data.frame(variable = c("12.6% ", "30.9%", "61.4%"))
as.numeric(sub('%$', '', df$variable))
[1]   NA 30.9 61.4
Warning message:
NAs introduced by coercion


<<<snip>>>


Dan

--
Daniel Nordlund
Port Townsend, WA  USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting chr to num

Rui Barradas
Hello,

Inline.

On 20/08/2018 01:08, Daniel Nordlund wrote:

> See comment inline below:
>
> On 8/18/2018 10:06 PM, Rui Barradas wrote:
>> 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 '%'.
>
> The pattern is more strict, and that could cause the conversion to fail
> if the process that created the strings resulted in trailing spaces.

That's true, and I had thought of that but it wasn't in the OP's problem
description.
The '$' could still be used with something like "%\\s*$":

as.numeric(sub('%\\s*$', '', df$variable))
#[1] 12.6 30.9 61.4


Rui Barradas


> Without the '$' the conversion succeeds.
>
> df <- data.frame(variable = c("12.6% ", "30.9%", "61.4%"))
> as.numeric(sub('%$', '', df$variable))
> [1]   NA 30.9 61.4
> Warning message:
> NAs introduced by coercion
>
>
> <<<snip>>>
>
>
> Dan
>

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting chr to num

Spencer Graves-4
       Have you considered "Ecfun::asNumericChar" (and
"Ecfun::asNumericDF")?


DF <- data.frame(variable = c("12.6% ", "30.9%", "61.4%", "1"))
Ecfun::asNumericChar(DF$variable)
[1] 0.126 0.309 0.614 1.000


       If you read the documentation including the examples, you will
see that many of these issues and others are handled automatically in
the way that I thought was the most sensible.  If you disagree, we can
discuss other examples and perhaps modify the code for those functions.


       Spencer Graves


On 2018-08-20 00:26, Rui Barradas wrote:

> Hello,
>
> Inline.
>
> On 20/08/2018 01:08, Daniel Nordlund wrote:
>> See comment inline below:
>>
>> On 8/18/2018 10:06 PM, Rui Barradas wrote:
>>> 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 '%'.
>>
>> The pattern is more strict, and that could cause the conversion to
>> fail if the process that created the strings resulted in trailing
>> spaces.
>
> That's true, and I had thought of that but it wasn't in the OP's
> problem description.
> The '$' could still be used with something like "%\\s*$":
>
> as.numeric(sub('%\\s*$', '', df$variable))
> #[1] 12.6 30.9 61.4
>
>
> Rui Barradas
>
>
>> Without the '$' the conversion succeeds.
>>
>> df <- data.frame(variable = c("12.6% ", "30.9%", "61.4%"))
>> as.numeric(sub('%$', '', df$variable))
>> [1]   NA 30.9 61.4
>> Warning message:
>> NAs introduced by coercion
>>
>>
>> <<<snip>>>
>>
>>
>> Dan
>>
>
> ---
> This email has been checked for viruses by AVG.
> https://www.avg.com
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.