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
Hello,
Your earch pattern is wrong, it should be
gsub("\\.", ", ", x[9])
I find x[9] a bit strange, by the way. Specially if the column vector
name is V16. Anyway, try the instruction above and if it doesn't work,
post a data example with
dput( head(x, 16) ) # paste the output of this in a post
Hope this helps,
Rui Barradas
Em 22-08-2012 18:24, Paula Cafeld escreveu:> 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.
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.
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.
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 >