Hello, I'm running version R x64 v2.12.2 on a 64bit windows 7 PC. I'm trying to read a text file using read.table where the values have a format like "1,234,567". What I want is "1234567". Is there a quick way to strip out the commas? I can use strsplit and paste, but the file is quite large and would take some time. Thanks. Eric [[alternative HTML version deleted]]
Hi Eric, Try this: gsub(",", "", "1,234,567", fixed = TRUE) Cheers, Josh On Mon, Jun 13, 2011 at 8:48 AM, Lee, Eric <elee at air-worldwide.com> wrote:> Hello, > > I'm running version R x64 v2.12.2 on a 64bit windows 7 PC. ?I'm trying to read a text file using read.table where the values have a format like "1,234,567". ?What I want is "1234567". ?Is there a quick way to strip out the commas? ?I can use strsplit and paste, but the file is quite large and would take some time. ?Thanks. > > Eric > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Peter Alspach
2011-Jun-13 20:34 UTC
[R] remove commas in a number when reading a text file
Tena koe Eric ?sub and ?gsub HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Lee, Eric > Sent: Tuesday, 14 June 2011 3:49 a.m. > To: r-help at R-project.org > Subject: [R] remove commas in a number when reading a text file > > Hello, > > I'm running version R x64 v2.12.2 on a 64bit windows 7 PC. I'm trying > to read a text file using read.table where the values have a format > like "1,234,567". What I want is "1234567". Is there a quick way to > strip out the commas? I can use strsplit and paste, but the file is > quite large and would take some time. Thanks. > > Eric > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited.
Peter Langfelder
2011-Jun-13 20:34 UTC
[R] remove commas in a number when reading a text file
On Mon, Jun 13, 2011 at 8:48 AM, Lee, Eric <elee at air-worldwide.com> wrote:> Hello, > > I'm running version R x64 v2.12.2 on a 64bit windows 7 PC. ?I'm trying to read a text file using read.table where the values have a format like "1,234,567". ?What I want is "1234567". ?Is there a quick way to strip out the commas? ?I can use strsplit and paste, but the file is quite large and would take some time. ?Thanks.You could use gsub. if you have a character string s, use sWithoutCommas = gsub(",", "", s, fixed = TRUE) to remove all commas from s. To do it for a whole table, I would do something like removeComma= function(s) {gsub(",", "", s, fixed = TRUE)} tabWithoutCommas = apply(tab, 2, removeComma) Try it to see if does what you need. HTH, Peter
The following link may be of interest. It shows a way to use gsub, but have it automatically applied while reading the data in rather than converting after: http://finzi.psych.upenn.edu/Rhelp10/2010-February/229550.html -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Lee, Eric > Sent: Monday, June 13, 2011 9:49 AM > To: r-help at R-project.org > Subject: [R] remove commas in a number when reading a text file > > Hello, > > I'm running version R x64 v2.12.2 on a 64bit windows 7 PC. I'm trying > to read a text file using read.table where the values have a format > like "1,234,567". What I want is "1234567". Is there a quick way to > strip out the commas? I can use strsplit and paste, but the file is > quite large and would take some time. Thanks. > > Eric > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.