[R] gsub -> replace substring in column

MacQueen, Don macqueen1 at llnl.gov
Wed Aug 22 21:39:29 CEST 2012


This is untested, but I suspect you should try
  x[[9]]
instead of
  x[9]

If you want to replace the original values with the modified values, then
you will need something like,

  x[[9]] <- gsub(".",",",x[[9]],fixed=T)

The difference between single brackets [] and double brackets [[]] is
important in R. See
  help('[')

Did you intend for "GPS LATITUDE" to be part of the data?
If not, look at the "skip" argument to read.csv().

You might look into using read.delim() instead of read.csv(), since
read.delim() already has sep='\t' (it's not a CSV file if the separator is
a tab character).  

If you want your latitudes to be numeric, then changing "." to "," will
not help. Somewhere you set an option for whether numeric data is
displayed using a "." or a ",".

Hope this helps.

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 8/22/12 10:24 AM, "Paula Cafeld" <paula.cafeld at yahoo.de> 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:
>
>> 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
>
>
>______________________________________________
>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.




More information about the R-help mailing list