Re: [R] Converting chr to num

2018-08-20 Thread Spencer Graves
  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

Re: [R] Converting chr to num

2018-08-19 Thread 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 =

Re: [R] Converting chr to num

2018-08-19 Thread Daniel Nordlund
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

Re: [R] Converting chr to num

2018-08-18 Thread Rui Barradas
uot;,"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] Converting chr to num Hey there,

Re: [R] Converting chr to num

2018-08-18 Thread GALIB KHAN
%","","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.

Re: [R] Converting chr to num

2018-08-18 Thread GALIB KHAN
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

Re: [R] Converting chr to num

2018-08-18 Thread Bert Gunter
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 > > > >

Re: [R] Converting chr to num

2018-08-18 Thread Jeff Reichman
: 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

Re: [R] Converting chr to num

2018-08-18 Thread Rui Barradas
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] Converting chr to num

2018-08-18 Thread Jeff Reichman
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