1

I have received a field with XY coordinates that have a decimal as a comma, but I cannot correctly open this file in R.

Here is how my data should look like - note comma as decimal indicator in y coordinates:

"OBJECTID";"x_coord";"y_coord"
"1";"664936,3059";"5582773,2319"   # comma as separator
"2";"604996,5803";"5471445,4964"
"3";"772846,82";"5353980,45"
"4";"552181,8639";"5535271,7626"
"5";"604022,9011";"5470134,0649"

But, specifying the dec = ',' in read.csv just reads it as a normal values:

xy <- read.delim(paste(path, "test_decimals2.txt", sep = '/'), 
               sep = '\t', dec = ",",skip = 0)

Missing comma separator from y coordinates:

  OBJECTID    x_coord     y_coord
1        1 6649363059 55827732319   # not a comma separator anymore
2        2 6049965803 54714454964
3        3   77284682   535398045
4        4 5521818639 55352717626
5        5 6040229011 54701340649

I have tried to convert the data to txt, etc. but still have the same problem. Do someone know how to make sure that dec = ',' will work correctly? Thank you!

(coordintaes are in UTM, that's why they look a bit weird)

maycca
  • 3,848
  • 5
  • 36
  • 67
  • On my env, `read.delim(PATH, sep = ';', dec = ",", skip = 0)` doesn't work . (I think this question is reproducible). – cuttlefish44 Jan 26 '22 at 10:02

5 Answers5

2

I copied the data into a txt file and this worked just fine for me:

read.csv2("test_decimals2.txt")

Out:

  OBJECTID  x_coord y_coord
1        1 664936.3 5582773
2        2 604996.6 5471445
3        3 772846.8 5353980
4        4 552181.9 5535272
5        5 604022.9 5470134
Kjetil Haukås
  • 374
  • 1
  • 11
  • Indeed, that was the one that worked for me: just save it in another format, and remove 'sep' indicator on the first line to correctly read the rest. Thank you! – maycca Feb 03 '22 at 14:38
1
a <- read.csv2("test_decimals2.txt", dec = ",", as.is = TRUE, header = FALSE, skip = 1)
a |> mutate(X = sub(",", ".", V2), Y = sub(",", ".", V3)) |>
  select(V1, X, Y)
Grzegorz Sapijaszko
  • 1,913
  • 1
  • 5
  • 12
1

I think read.csv2 is the answer for those european style csv-Data:

xy <- read.csv2(file = paste(path, "test_decimals2.txt", sep = '/'))

Your approach might also be correct. The data is correct, it just does not display enough digits in the print.

Try printing your data with:

> print.data.frame(xy, digits = 10)
  OBJECTID     x_coord     y_coord
1        1 664936.3059 5582773.232
2        2 604996.5803 5471445.496
3        3 772846.8200 5353980.450
4        4 552181.8639 5535271.763
5        5 604022.9011 5470134.065

This prints the dataframe with a limit of ten digits.

Sandwichnick
  • 1,379
  • 6
  • 13
1

Adding to the previous answers. If you want to use read.delim() function you can simply type:

xy <- read.delim(file_path, sep = ';', skip = 0, dec = ",")

OR

xy <- read.delim(file_path, sep = ';', skip = 0, colClasses = "character")
xy$x_coord <- as.numeric(gsub(",", ".", xy$x_coord))
xy$y_coord <- as.numeric(gsub(",", ".", xy$y_coord))

Then, you may change the default display use to print more digits by using:

options(digits = 12)

Than, printing the data.frame would lead to :

  OBJECTID     x_coord      y_coord
1        1 664936.3059 5582773.2319
2        2 604996.5803 5471445.4964
3        3 772846.8200 5353980.4500
4        4 552181.8639 5535271.7626
5        5 604022.9011 5470134.0649
DoRemy95
  • 614
  • 3
  • 19
1

One possible approach:

testcommas <- read_delim(path,"\t", col_types=cols(.default="c"))
testcommas
# A tibble: 4 x 3
  item  coord_y coord_x
  <chr> <chr>   <chr>  
1 1     156,158 543,697
2 2     324,678 169,385
3 3     097,325 325,734
4 4     400,211 158,687

With this, all columns will be of type "character". Then you may change them to numeric if/as needed. One way:

testcommas <- data.frame(sapply(testcommas, function(x)  as.numeric(sub(",", ".", x, fixed = TRUE))))
testcommas
  item coord_y coord_x
1    1 156.158 543.697
2    2 324.678 169.385
3    3  97.325 325.734
4    4 400.211 158.687
Dharman
  • 30,962
  • 25
  • 85
  • 135
Valentia
  • 46
  • 3