[R] gsub -> replace substring in column

David Winsemius dwinsemius at comcast.net
Thu Aug 23 02:21:49 CEST 2012


On Aug 22, 2012, at 10:24 AM, Paula Cafeld wrote:

> Hi all,
>
> please excuse- I'm a complete newbie to R, so it's possible my  
> question was asked a thousand times before, but I don't get it :-(
> I imported a CSV file via:
>
> x=read.csv("test.csv",header=TRUE,sep="\t")
>
> In a column there are values with the dot-character (".") I want to  
> replace with a commata:

You should NOT be trying to do that. R will not recognize such values  
as proper numeric values. If you had such values in a text file you  
could be using read.csv2 to read them, but once inside R, they will  
get displayed with periods. write.table() has a 'dec' argument that  
lets you write out results in your preferred format. There are many  
options for changing the way the R behaves, but I do not believe that  
changing the decimal-point symbol is one of them. Trying typing and  
read this:

names(options())
?options

David Winsemius, MD
Alameda, CA, USA

>
>> x[9]
>                    V16
> 1         GPS LATITUDE
> 2    53.51982466427600
> 3   51.520379571037000
> 4   53.520745152837800
> 5   51.521750487103766
> 6    53.52067987059652
> 7   53.519504773664345
> 8    51.51861690180330
> 9   51.519100010776675
> 10   51.51905431150669
> 11    51.5193415632712
> 12   53.51927627894419
> 13   51.52073862461807
> 14   50.51989647613406
> 15   50.51789875702557
> 16   50.52051666456883
>
> So I tried:
>
>> cat(gsub(".",",",x[9],fixed=T))
>
> which outputs:
>
> c(111, 79, 81, 85, 87, 83, 78, 72, 75, 74, 77, 76, 84, 80, 70, 82,  
> 112, 112, 1, 1, 1, 1, 1, 1, 1, 1, 98, 95, 105, 92, 89, 94, 101, 103,  
> 104, 107, 106, 108, 109, 110, 1, 1, 1, 100, 99, 102, 97, 96, 93, 91,  
> 90, 88, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 16, 23, 25, 21, 22,  
> 19, 20, 24, 17, 14, 15, 18, 1, 26, 28, 27, 30, 29, 32, 34, 39, 54,  
> 57, 73, 44, 42, 56, 53, 49, 63, 52, 45, 55, 1, 1, 1, 1, 1, 65, 51,  
> 61, 59, 86, 31, 67, 60, 35, 41, 38, 40, 33, 37, 36, 43, 62, 58, 64,  
> 68, 69, 66, 50, 47, 71, 46,
> 48, 1, 1, 1, 1, 1, 13, 12, 11, 11, 7, 1, 1, 8, 9, 1, 4, 2, 5, 6, 1,  
> 1, 1, 10, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  
> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
>
> The values in the column are still unchanged. What have I to do that  
> the dot will be replaced with the commata?
>
> Thanks in advanced
> Paula
>




More information about the R-help mailing list