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]]
Hey there, as.numeric(gsub(pattern = "%","","12.6%")) On Sat, Aug 18, 2018 at 4:20 PM, Jeff Reichman <reichmanj at 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 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. >[[alternative HTML version deleted]]
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]] > > ______________________________________________ > 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. >--- This email has been checked for viruses by AVG. https://www.avg.com
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 at scarletmail.rutgers.edu> Sent: Saturday, August 18, 2018 4:23 PM To: reichmanj at sbcglobal.net Cc: r-help at 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 at sbcglobal.net <mailto:reichmanj at 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 at r-project.org <mailto: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. [[alternative HTML version deleted]]
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 <reichmanj at 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 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. >[[alternative HTML version deleted]]