[R] gsub -> replace substring in column

arun smartpink111 at yahoo.com
Thu Aug 23 01:15:07 CEST 2012


Hi,
Try this:
x<-read.table(text="
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
",sep="",header=TRUE)x1<-data.frame(GPS_LATITUDE=x[,-1])
 rownames(x1)<-1:nrow(x1)
 data.frame(GPS_LATITUDE=gsub("(\\d+).(\\d+)","\\1,\\2",x1$GPS_LATITUDE))
#       GPS_LATITUDE
#1   53,519824664276
#2   51,520379571037
#3  53,5207451528378
#4  51,5217504871038
#5  53,5206798705965
#6  53,5195047736643
#7  51,5186169018033
#8  51,5191000107767
#9  51,5190543115067
[.....]
A.K.




----- Original Message -----
From: Paula Cafeld <paula.cafeld at yahoo.de>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Wednesday, August 22, 2012 1:24 PM
Subject: [R] gsub -> replace substring in column

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