Shubha Vishwanath Karanth
2007-Sep-24 08:13 UTC
[R] Data manipulations with numbers which are in 'comma' format
Hi R, May be a trivial question, but struggling to find a solution... v=data.frame(a=c("1,234","2,345","5,567"))> va 1 1,234 2 2,345 3 5,567 I need a column 'b', which is just the addition of column 'a' with 5. How do I do it? And, entries in column 'a' are with commas, always. Also, class(v$a)=factor. BR, Shubha [[alternative HTML version deleted]]
Petr PIKAL
2007-Sep-24 09:24 UTC
[R] Odp: Data manipulations with numbers which are in 'comma' format
Hi> Hi R, > > > > May be a trivial question, but struggling to find a solution... > > > > v=data.frame(a=c("1,234","2,345","5,567")) > > > v > > a > > 1 1,234 > > 2 2,345 > > 3 5,567 > > > > I need a column 'b', which is just the addition of column 'a' with 5. > How do I do it? And, entries in column 'a' are with commas, always. > Also, class(v$a)=factor.something like> v$b <- as.numeric(gsub(",", ".", v$a)) + 5 > va b 1 1,234 6.234 2 2,345 7.345 3 5,567 10.567 Regards Petr> > > > BR, Shubha > > > [[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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Julien Barnier
2007-Sep-24 09:32 UTC
[R] Data manipulations with numbers which are in 'comma' format
Hi,> I need a column 'b', which is just the addition of column 'a' with 5. > How do I do it? And, entries in column 'a' are with commas, always. > Also, class(v$a)=factor.You must convert your factor with commas into a numeric variable, first replacing commas with dots. A very dirty way to do it could be the following, but there may be a better one : v$a <- as.numeric(gsub(",",".",as.character(v$a))) HTH, Julien -- Julien Barnier Groupe de recherche sur la socialisation ENS-LSH - Lyon, France
Jean lobry
2007-Sep-24 19:11 UTC
[R] Data manipulations with numbers which are in 'comma' format
Dear Shubha,> May be a trivial question, but struggling to find a solution... > > v=data.frame(a=c("1,234","2,345","5,567")) > >> v > > a > 1 1,234 > 2 2,345 > 3 5,567 > > I need a column 'b', which is just the addition of column 'a' with 5. > How do I do it? And, entries in column 'a' are with commas, always. > Also, class(v$a)=factor.if you are working (as me) in a locale where the comma is the decimal separator, you should be awared that R is *very* good to cope with this nightmare: ### a <- c("1,234", "2,345", "5,567") v <- read.table(textConnection(a), col.names = "a", dec = ",") v$b <- v$a + 5 v ### a b 1 1.234 6.234 2 2.345 7.345 3 5.567 10.567 Have a look at the "dec" argument in ?read.table. See also ?format. Wonderful R, isn't it? Best, -- Jean R. Lobry (lobry at biomserv.univ-lyon1.fr) Laboratoire BBE-CNRS-UMR-5558, Univ. C. Bernard - LYON I, 43 Bd 11/11/1918, F-69622 VILLEURBANNE CEDEX, FRANCE allo : +33 472 43 27 56 fax : +33 472 43 13 88 http://pbil.univ-lyon1.fr/members/lobry/