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 the
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 = "%"
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(
pattern = "%","","12.6%"))
To the variable name say … as.numeric(gsub(pattern = "%","",df$variable))
*From:* GALIB KHAN
*Sent:* Saturday, August 18, 2018 4:23 PM
*To:* reichm...@sbcglobal.net
*Cc:* r-help@r-project.org
*Subject:* Re: [R] Con
quot;%","","12.6%"))
>
> To the variable name say … as.numeric(gsub(pattern = "%","",df$variable))
>
>
>
>
>
> *From:* GALIB KHAN
> *Sent:* Saturday, August 18, 2018 4:23 PM
> *To:* reichm...@sbcglobal.net
> *Cc:* r-help@r-pro
Hey there,
as.numeric(gsub(pattern = "%","","12.6%"))
On Sat, Aug 18, 2018 at 4:20 PM, 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 vers
u 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
wrote:
> R-Help Forum
>
>
>
> How
: reichm...@sbcglobal.net
Cc: r-help@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 mailto:reichm...@sbcglobal.net> > wrote:
R-Help Forum
How do
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
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@r-project.org mailing list -- To UNSUBSCRIBE and more, see
ht
10 matches
Mail list logo