[R] Data manipulations with numbers which are in 'comma' format

Jean lobry lobry at biomserv.univ-lyon1.fr
Mon Sep 24 21:11:18 CEST 2007


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/



More information about the R-help mailing list